Traversing an image matrix columnwise

9 ビュー (過去 30 日間)
Karan Ratnaparkhi
Karan Ratnaparkhi 2011 年 3 月 31 日
Hi guys i wanna traverse a matrix of an image column wise ie. it should consider first column and visit all the rows of that column and so on using for loops. Plz help me if u know the logic or code.

採用された回答

Josh
Josh 2011 年 3 月 31 日
If I understand your question, the solution is a nested for loop along the lines of
[rows,cols] = size(Matrix);
for col = 1:cols
for row = 1:rows
work with Matrix(row,col);
end
end
  2 件のコメント
MechtEngineer
MechtEngineer 2011 年 3 月 31 日
Exactly the same as mine above, except neater!
Karan Ratnaparkhi
Karan Ratnaparkhi 2011 年 3 月 31 日
thnx josh n mark..

サインインしてコメントする。

その他の回答 (2 件)

MechtEngineer
MechtEngineer 2011 年 3 月 31 日
I think you would want the following, assuming an m x n matrix A(row x column) with position of each array point (i,j)
for j = 1:size(A,2) % for each column
for i = 1:size(A,1) % for each row
% put your operation in here - I am just adding 1 to each pixel as an eg.
A(i,j) = A(i,j) + 1;
end
end

David Young
David Young 2011 年 3 月 31 日
Linear indexing uses columnwise ordering, so as long as you don't need the row and column indexes explicitly, you can do
for k = 1:numel(A)
% operate on A(k)
end
It might be even better to consider whether you can vectorise the operation and avoid the loops altogether.
  1 件のコメント
MechtEngineer
MechtEngineer 2011 年 3 月 31 日
Impressive solution! That really shortens the code required, and does the same thing.
And yes, as David Young says above, if you vectorise (or make the desired operations into matrices to add, subtract, multiply, correlate, etc) the operation, it will be quicker than "for" loops - I tried it recently, and my code ran 5 times faster!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by