Can't get simple for loop to run

1 回表示 (過去 30 日間)
Alex Hughes
Alex Hughes 2020 年 9 月 22 日
回答済み: Deepak Meena 2020 年 9 月 25 日
I've still not got the hang of loops! I have a 3667 x 1 double matrix (d1) and I have a 33 x 1 matrix (d2). All the values in the 33 x 1 matrix (d2) should correpsond to values in the 3667 x 1 matrix (d1). I want to loop throught each row of the the 33 x 1 matrix and get the index of the equivelant value in the 3667 x 1 matrix. I can do this without a loop using the following code using just one value from d2:
index_1=find(d1(:,1)==R1);
where R1 correpsonds to the integer value in the matrix d2 (e.g. d2(1,1))
However, when I try and put this in a loop the result ij is empty. The code seemd to run but I'm not getting the result that I get outside the loop.
size_d2=size(d2);
results=zeros(size(1,1),1);
for ii=d2(1):d2(end)
ij=find((d1(:,1))==ii);
results(ii)=ij;
end
Any advice on where I'm going wrong would be appreciated!
Thanks

回答 (1 件)

Deepak Meena
Deepak Meena 2020 年 9 月 25 日
Hi Alex ,
I think you are defining "results " variable wrong. From my understanding you want to results variable to be similar size of as d2. so you should be doing this :
results=zeros(size_d2(1,1),1);
Rest of the code seems fine to me.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by