How can I store index number and value from a for loop and a problem with optimization

4 ビュー (過去 30 日間)
Bran
Bran 2013 年 4 月 30 日
My problem is two-fold;
I am attempting to store the minimum value from each row of a matrix that is 3x4193 in dimensions;
for i = 1:3
tmp(i) = rand;
dummy(i,:) =(abs(tmp(i) - cumsum_pdf_x));
[val ind] = min(dummy(i,:));
end
I have used to above code but it only gives me min value and corresponding index for one of the rows. How can I get and store the values for all three? Also I then have to increase i to 2915, how can I optimize my code so that it does this easily as it is at the moment causing matlab to crash?
Many thanks in advance :D

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 30 日
編集済み: Azzi Abdelmalek 2013 年 4 月 30 日
for i = 1:3
tmp(i) = rand;
dummy(i,:) =abs(tmp(i) - cumsum_pdf_x);
[val1 ind1] = min(dummy(i,:));
val(i)=val1;
ind(i)=ind1
end

Matt J
Matt J 2013 年 4 月 30 日
編集済み: Matt J 2013 年 4 月 30 日
Do the minimization outside the loop, in vectorized fashion.
for i = 1:3
tmp(i) = rand;
dummy(i,:) =abs(tmp(i) - cumsum_pdf_x);
end
[val,ind] = min(dummy,[],2);
As for the "crash", we need to see error messages to diagnose that.

カテゴリ

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