store values in array in nested for loop

ss=1;
for i = 1:2
for j = 1:5
T = norm(r2(j,:)-r1(i,5:end)); % reading 2 excel files and r2 and r1 are rows
RS(ss) = T
ss = ss + 1;
end
[r,c]=min(RS)
clearvars RS
end
end
As i run the code I got this result after 1 iteration
RS =
613.3480 566.6174 614.9722 644.0272 627.2652
r =
566.6174
c =
2
but as i=2 it displays
RS =
0 0 0 0 0 602.8387 522.1276 605.9619 535.2905 468.5696
r =
0
c =
1
I want to know the min value from RS at each iteration and i want to have
RS =
602.8387 522.1276 605.9619 535.2905 468.5696
with no zeros in my array,so that i get the correct min value. Thankx

1 件のコメント

per isakson
per isakson 2014 年 9 月 13 日
What values do r1 and r2 have?

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

 採用された回答

Roger Stafford
Roger Stafford 2014 年 9 月 13 日

1 投票

To get the correct minimum values, replace "clearvars RS" with "ss=1;".
If you want to retain a record of these two minimums and their indices, you need to store them in arrays at the end of each of the two passes in the outer for-loop.

1 件のコメント

Muhammad Usman
Muhammad Usman 2014 年 9 月 14 日
Thankyou so much, i am very grateful for you. your solution works for me

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 9 月 13 日
編集済み: Andrei Bobrov 2014 年 9 月 13 日

0 投票

[r,c] = min(squeeze(sqrt(...
sum(bsxfun(@minus,r2(1:5,:),permute(r1(1:2,5:end),[3 2 1])).^2,2))));

カテゴリ

ヘルプ センター および 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