我对MATLAB是个新手,对此我不太确定。我有一个矩阵
matrix = [1 2 3 4;8 9 5 6];
现在,我想遍历上述矩阵的列,并检索当前列之前的列。因此,如果在迭代时,我们在第2列,那么我们应该检索第1列。
有人能给我指个方向吗?我试过了
for v = matrix
disp(v-1)
end
但这并不管用。任何帮助都将不胜感激。
发布于 2011-10-09 15:10:43
首先,我们需要找出矩阵中有多少列:
m = [1,2,3,4;9,8,5,6]
[rows, cols] = size(m)
接下来,我们将遍历所有列并打印出当前列:
for ii=1:1:cols
disp('current column: ')
m(:,ii) % the : selects all rows, and ii selects which column
end
如果您想要上一列,而不是当前列:
for ii=1:1:cols
if ii == 1
disp('first column has no previous!')
else
disp('previous column: ')
m(:,ii-1) % the : selects all rows, and ii selects columns
end
end
https://stackoverflow.com/questions/7704368
复制