Subscripted assignment dimension mismatch.

Hello all,
I know this is a common error and their seem to be millions of solutions for it however I can't get my code to run. I have read all the example I can find but non seem to help. this is my code:
a1=imread('1V.png'); a2=imread('2V.png'); a3=imread('3V.png'); a(:,:,1)=a1; a(:,:,2)=a2; a(:,:,3)=a3;
for t=1:3; y=a(:,:,t); Isum=sum(y,1); [Imax, Iloc]=max(Isum); I(t)=y(:,Iloc); end
The code runs fine if I place a single value in for t, however when I run it as a loop the subscript dimensions mismatch error pops up. I want the code to give me a matrix containing all of the I values, three columns in this case.

 採用された回答

KL
KL 2017 年 12 月 11 日
編集済み: KL 2017 年 12 月 11 日

0 投票

imread returns a 3D matrix. If you want to read multiple images and store them in one matrix, use a 4D matrix,
for k = 1:3
a(:,:,:,k) = imread([num2str(k) 'V.png']);
%other code
end

4 件のコメント

Robbie McDermott
Robbie McDermott 2017 年 12 月 11 日
Thank you for your reply!
The code tells me that the imaread matrices are all 2D though. When I say size(a1) it returns 1280 960
KL
KL 2017 年 12 月 11 日
Aha, then you're reading grayscale images. The the problem should be in assiging l(t). Change it to l(:,t)
You've been actually trying to assign a vector to a single element. Alternative is to use cell arrays. l{t}.
Robbie McDermott
Robbie McDermott 2017 年 12 月 11 日
AH! Using l(:,t) has fixed it! Thank you very very much! that will save me a lot of time!
Thank you so much, very very much appreciated!!
KL
KL 2017 年 12 月 11 日
編集済み: KL 2017 年 12 月 11 日
You're very welcome!
Another tip: Always try to preallocate your matrices before loop, this would greatly improve your code's performance. For example,
l = zeros(256,3);

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2017 年 12 月 11 日

編集済み:

KL
2017 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by