Reputation: 29
I could get the minimum value and its index like in here matlab how to get min value and its index in a matrix.
From a matrix A
A=[1 3 6 2 0 4
6 8 9 5 1 3
7 2 7 8 9 2]
To get the minimal value MinVal
(where the row is given (r) and the column is in an interval ([c.. c+x]) and the index ind
(number of column of it)). I have to do
[MinVal,I]=min(A(r,c:c+x))
ind= c-1+I;
Example
[MinVal, ind]=min(A(2,3:3+2))
will give me
% MinVal= 1
% ind =5
Then I have
B.state=[ 0
0
1
0
1]
So here I
can't take ind=5
because B(5).state==1
, I need to move to the next MinVal= 5
and ind = 4
. Here, it is ok, I can stop but if B(4).state ==1
, then I need to move the next smallest and so on
But then the problem is that I have another structure B
where I am going to check if B(ind).state== 1
then I have to move to the next smallest element and get its index and so on until I find the first empty one.
If I try like this
MinD = A(r,c:c+x);
[MinVal,Ind]=min(MinD);
ind= nbrT+Ind;
MinD2 = sort(MinD(:));
p=2;
while (B(ind).state == 1)
MinVal= MinD2(p);
%need to get the new index
%something like this
ind=find (A == MinVal) ;
p=p+1;
end
The problem is that I can get the next minimum value but the index I will get can be of more than one value if MinVal appears more than once so how can I get the one with state == 0
I don't want to use unique either because even if I have two different elements with the same minimum, they refer to two different places and I have to keep both (I can use the second one if the first one is full).
Modified code to
MinD = A(r,c:c+x);
[MinVal,Ind]=min(MinD);
ind= nbrT+Ind;
[MinD2, IndMinD2] = sort(MinD(:));
p=2;
while (B(ind).state == 1)
MinVal=MinD2(p);
Ind=IndMind2(p);
p=p+1;
end
Ind= c-1+Ind;
So how can I do it?
Upvotes: 0
Views: 202
Reputation: 2347
I think this should work:
MinD2 = MinD(:));
for ii=1:numel(MinD2)
[MinVal,Ind]=min(MinD2);
%do you stuff with the index
%and at the end do this:
MinD2(Ind)=Inf;
end
Upvotes: 0