循环部分矩阵
Loop over part of Matrix
我有一个矩阵 A。我想遍历矩阵 (B) 的内部,同时处理不属于 B 的行和列。
A = [1 4 5 6 7 1; B = [2 2 2 2;
8 2 2 2 2 1; 2 3 3 2;
9 2 3 3 2 1; 2 8 2 2];
0 2 8 2 2 1;
1 1 1 1 1 1];
我知道可以 select A 的部分像这样:
[rows,columns] = size(A);
B = A([2:1:rows-1],[2:1:columns-1]);
for i = 1:(rows*columns)
%do loop stuff
endfor
但这不会起作用,因为我还需要外部行和列来进行计算。如何在不改变 A 的情况下实现循环?
那么,为什么不对内矩阵使用两个索引呢?
%....
for i=2:rows-1
for j=2:cols-1
% here, A(i,j) are the B elements, but you
% can still access to A(i-1, j+1) if you want.
end
end
%....
我有一个矩阵 A。我想遍历矩阵 (B) 的内部,同时处理不属于 B 的行和列。
A = [1 4 5 6 7 1; B = [2 2 2 2;
8 2 2 2 2 1; 2 3 3 2;
9 2 3 3 2 1; 2 8 2 2];
0 2 8 2 2 1;
1 1 1 1 1 1];
我知道可以 select A 的部分像这样:
[rows,columns] = size(A);
B = A([2:1:rows-1],[2:1:columns-1]);
for i = 1:(rows*columns)
%do loop stuff
endfor
但这不会起作用,因为我还需要外部行和列来进行计算。如何在不改变 A 的情况下实现循环?
那么,为什么不对内矩阵使用两个索引呢?
%....
for i=2:rows-1
for j=2:cols-1
% here, A(i,j) are the B elements, but you
% can still access to A(i-1, j+1) if you want.
end
end
%....