array and minimum value and coordinate
3 次查看(过去 30 天)
显示 更早的评论
Hi, everyone, first I want to explain my code.
that is, I am doing some iteration but iteration is done according to some criteria. I have 5 criteria and each criteria there some iterations. Program starts, criteria is selected then iterations is done. But after each iteration, error is calculated according referance value. So that I want to put all error in array and find their minimum. But I also have to know that minumin error belongs which criteria and which iteration.
can u help me about this ?
king regards
1 个评论
回答(1 个)
dpb
2014-4-26
Basically, preallocate an array of the size of number of criterion (5) by the number of pieces of data you want to keep (in your description I see
- the iteration number/identifier
- the error for the particular criterion
So,
errarray=zeros(5,2); % allocate room
index=0; % an index variable for which we're on...
%do iteration one here...
index=index+1;
errarray(index,1)=iteration; % save the iteration value
errarray(index,2)=result-refvalue; % and the error
%do iteration two here...
index=index+1;
errarray(index,1)=iteration; % save the iteration value
errarray(index,2)=abs(result-refvalue); % and the error
% lather, rinse, repeat...
When done w/ all five, then
errarray=sortrows(errarray,2); % sort by error column
error=errarray(1,:); % the first row is the minimum and the aux value with it
You can also use
[emin,idx]=min(errarray(:,1)); % find the minimum and location thereof
error=errarray(idx,:); % idx is the row of the minimum
If your repeat is a loop or a nest of multiple loops, just rearrange the linear path above within that structure.
2 个评论
dpb
2014-4-26
Add enough dimensions to account for the number of variables need or use cell arrays to hold different numbers of items per cell.
Need a better outline of the actual code as Azzi notes to do more than generic answers as given.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!