在球拍中制作矩阵块(由列表表示)

发布于 2025-01-16 09:53:22 字数 309 浏览 1 评论 0原文

我有一个任务,需要将给定的矩阵划分为块(还给出了宽度和高度并适合矩阵)。矩阵由列表列表表示,其中每个列表都是矩阵中的一行。 例如,

'(( 1  2  3  4)
  ( 5  6  7  8)
  ( 9 10 11 12)
  (13 14 15 16))

对于给定的 width=height=2,块应为 (1 2 5 6)、(3 4 7 8)、(9 10 13 14)、(11 12 15 16)。

你能帮我吗?

我尝试按高度和宽度分割它,然后遍历并附加列表,但它根本不起作用,我不知道算法应该如何工作。

I have a task where I need to divide the given matrix into blocks (width and height are also given and suite the matrix). The metrix is represented by a list of lists, where every list is a row in the matrix.
For example

'(( 1  2  3  4)
  ( 5  6  7  8)
  ( 9 10 11 12)
  (13 14 15 16))

for given width=height=2 the blocks should be (1 2 5 6), (3 4 7 8), (9 10 13 14), (11 12 15 16).

Can you please help me with it?

I have trind to split it by height and width, then traverse and append lists, but it doenst work att all, I have no idea how the algorithm should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2025-01-23 09:53:22

您可以尝试这个代码 - 它可以工作,但看起来有点复杂,所以也许有更好的方法:

(define (partition-by n lst)
  (if (null? lst) lst
      (cons (take lst n)
            (partition-by n (drop lst n)))))

(define (chunk lst w)
  (map (lambda (group) (partition-by w group))
       lst))

(define (matrix->blocks matrix w h)
  (apply append
         (for/list ((row (partition-by w (chunk matrix w))))
           (partition-by (* w h) (flatten (apply map list row))))))

测试:

(define m '((1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)))

(matrix->blocks m 2 2)
=> '((1 2 5 6) (3 4 7 8) (9 10 13 14) (11 12 15 16))

You can try this code- it works, but it seems a little bit convoluted, so maybe there is some better way:

(define (partition-by n lst)
  (if (null? lst) lst
      (cons (take lst n)
            (partition-by n (drop lst n)))))

(define (chunk lst w)
  (map (lambda (group) (partition-by w group))
       lst))

(define (matrix->blocks matrix w h)
  (apply append
         (for/list ((row (partition-by w (chunk matrix w))))
           (partition-by (* w h) (flatten (apply map list row))))))

Test:

(define m '((1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)))

(matrix->blocks m 2 2)
=> '((1 2 5 6) (3 4 7 8) (9 10 13 14) (11 12 15 16))
好菇凉咱不稀罕他 2025-01-23 09:53:22

这个答案使用宽度和高度来指代块尺寸,并构建块矩阵
(每个块都是一个类似于输入的行列表,因此结果是一个 4 级列表;
如果需要较少的嵌套,可以适当插入展平)。
计划:将输入转换为平面向量,以正确的顺序获取元素以构建结果。

#lang racket

(require test-engine/racket-tests)
    
(define (matrix->matrixes lor w h) ;; (ListOf ListOfX) Natural Natural -> (ListOf (ListOf (ListOf ListOfX)))
  ;; produce matrix of blocks of size w x h from matrix lor, which is a list of rows
  ;; each block is (* w h) elements (list of h lists of length w)
  ;; eg (define M32 '((1 2) (3 4) (5 6)))  ; 3 rows, 2 cols
  ;;    (matrix->matrixes M32 2 1) => ( ( ((1 2)) ) ( ((3 4)) ) ( ((5 6)) ) )
  ;;    (matrix->matrixes M32 1 3) => ( ( ((1) (3) (5)) ((2) (4) (6)) ) )
  (let* ( [v   (list->vector (flatten lor))]
          [nr  (length lor)]         ;; n original rows
          [nc  (length (car lor))]   ;; n original cols
          [nrb (/ nr h)]             ;; n rows of blocks
          [ncb (/ nc w)])            ;; n cols of blocks
    (build-list nrb (lambda (brx)
        (build-list ncb (lambda (bcx)
            (build-list h (lambda (hx)
                (build-list w (lambda (wx)
                    (vector-ref v (+ (* brx (* ncb h w)) (* bcx w) (* hx nc) wx))))))))))))

(define M32 '((1 2) (3 4) (5 6)))
(define M44 '((1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)))

(check-expect (matrix->matrixes M32 2 1) '( ( ((1 2)) ) ( ((3 4)) ) ( ((5 6)) ) ) )
(check-expect (matrix->matrixes M32 1 3) '( ( ((1) (3) (5)) ((2) (4) (6)) ) ) )
(check-expect (matrix->matrixes M44 4 4) `(( ,M44 )))

(matrix->matrixes M44 2 2)

(test)

This answer takes width and height to refer to block dimensions, and builds a matrix of blocks
(each block is a list of rows like the input, so result is a rank 4 list;
if less nesting is required flattens can be inserted appropriately).
Plan: convert input to flat vector, fetch elements in correct order to build result.

#lang racket

(require test-engine/racket-tests)
    
(define (matrix->matrixes lor w h) ;; (ListOf ListOfX) Natural Natural -> (ListOf (ListOf (ListOf ListOfX)))
  ;; produce matrix of blocks of size w x h from matrix lor, which is a list of rows
  ;; each block is (* w h) elements (list of h lists of length w)
  ;; eg (define M32 '((1 2) (3 4) (5 6)))  ; 3 rows, 2 cols
  ;;    (matrix->matrixes M32 2 1) => ( ( ((1 2)) ) ( ((3 4)) ) ( ((5 6)) ) )
  ;;    (matrix->matrixes M32 1 3) => ( ( ((1) (3) (5)) ((2) (4) (6)) ) )
  (let* ( [v   (list->vector (flatten lor))]
          [nr  (length lor)]         ;; n original rows
          [nc  (length (car lor))]   ;; n original cols
          [nrb (/ nr h)]             ;; n rows of blocks
          [ncb (/ nc w)])            ;; n cols of blocks
    (build-list nrb (lambda (brx)
        (build-list ncb (lambda (bcx)
            (build-list h (lambda (hx)
                (build-list w (lambda (wx)
                    (vector-ref v (+ (* brx (* ncb h w)) (* bcx w) (* hx nc) wx))))))))))))

(define M32 '((1 2) (3 4) (5 6)))
(define M44 '((1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)))

(check-expect (matrix->matrixes M32 2 1) '( ( ((1 2)) ) ( ((3 4)) ) ( ((5 6)) ) ) )
(check-expect (matrix->matrixes M32 1 3) '( ( ((1) (3) (5)) ((2) (4) (6)) ) ) )
(check-expect (matrix->matrixes M44 4 4) `(( ,M44 )))

(matrix->matrixes M44 2 2)

(test)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文