Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
a4 = 10*magic(4); a5 = magic(5); a4 a5 diag4 = sub2ind([4,4], 1:3,1:3); diag5 = sub2ind([5,5], 1:3,1:3); a5(diag5) = a4(diag4) #Display changed contents diag4 %# Display diagonal of magic4 diag5 %# Display diagonal of magic5 a4(diag4)=a5(diag5) %# Recovering the original 输出是
a4 = %# Display of original a4 magic square 160 20 30 130 50 110 100 80 90 70 60 120 40 140 150 10 a5 = %#Display of original magic square 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 diag4 = 1 6 11 diag5 = 1 7 13 a5 = 160 24 1 8 15 23 110 7 14 16 4 6 60 20 22 10 12 19 21 3 11 18 25 2 9 a4 = 160 20 30 130 50 110 100 80 90 70 60 120 40 140 150 10 生成diag4和diag5的方式背后的逻辑是什么? 回答: 我对您的目标尚不完全清楚,这仍然是提取RGB图像对角线(每个颜色通道的2D矩阵对角线)的一种方法: A = rand(32,32,3); %# it can be any 3D matrix (and not necessarily square) [rcd] = size(A); diagIDX = bsxfun(@plus, 1:r+1:r*c, (0:d-1)'.*r*c); A( diagIDX(:) ) diagIDX将具有三行,每行包含对角元素的(线性)索引(每个切片一个)。从那里,您可以使其适应您的代码... 上面代码背后的想法很简单:采用2D矩阵,对角线元素可以使用以下方式访问: A = rand(5,4); [rc] = size(A); A( 1:r+1:r*c ) 然后在3D情况下,我添加了一个额外的偏移量,以相同的方式到达其他切片。 更多&回答... |
![]() |
![]() |