problem in for loop

3 ビュー (過去 30 日間)
NAVNEET NAYAN
NAVNEET NAYAN 2017 年 3 月 30 日
編集済み: Guillaume 2017 年 3 月 30 日
for i = 1:2:n
filename = strcat('I:\testing\8y\',num2str(i),'.jpg');
I = imread(filename);
I1 = rgb2gray(I);
mat1(:,:,i)= I1;
end
for j = 1:1:i-1
r1 = mat1(:,:,j);
r2 = mat1(:,:,j+1);
c = corr2(r1,r2);
mat2(j) = c;
end
When I run this code I got absurd results having zeros at alternate positions. most probably the reason is absence of even images in mat1(:,:,i) since i takes only odd values. Then I made a change in r2 as r2 = mat1(:,:,j+2), but it produced an error as "index exceeds matrix dimension". What should be the solution of this problem, so that r1 and r2 read odd values of i in a regular manner.
thanks
Navneet nayan

採用された回答

Jan
Jan 2017 年 3 月 30 日
Would it help to use
for j = 1:1:i-2 % Instead of ...-1
...
r2 = mat1(:,:,j+2);
?
  2 件のコメント
NAVNEET NAYAN
NAVNEET NAYAN 2017 年 3 月 30 日
yeah...it's working now... thanks a lot Simon
Guillaume
Guillaume 2017 年 3 月 30 日
The for loop should be
for j = 1:2:i-2 %step of 2!
for the code to make sense.

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

その他の回答 (1 件)

Guillaume
Guillaume 2017 年 3 月 30 日
編集済み: Guillaume 2017 年 3 月 30 日
What is the point of having a matrix mat1 twice the size it needs to be, storing lots of useless 0. Why not decouple the iterating of the files from the filling of the matrix:
imageindices = 1:2:n;
for i = 1:numel(imageindices)
filepath = fullfile('I:\testing\8y', sprintf('%d.jpg', imageindices(i))); %prefer fullfile to strcat for building paths
mat1(:, :, i) = rgb2gray(imread(filepath));
end
The second loop is then trivial:
for i = 1:size(mat1, 3)-1
mat2(i) = corr(mat1(:, :, i), mat1(:, :, i+1));
end

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by