minimum of an array
Show older comments
Hi, I have a 10*10 array.I need to find the minium of all the rows of the array except selected ones.For eg. to find the minimum of the array except 2nd and fifthrow.
Accepted Answer
More Answers (3)
Sean de Wolski
on 12 Dec 2011
A = [999, 25.019, 1.414, 25.806, 1.414, 3.316, 4.472, 29.782, 26.248, 28.248
25.019, 999, 25.059, 5.291, 25.495, 25.278, 25.573, 8.774, 3.316, 7.348
1.414, 25.059, 999, 25.612, 2.449, 4.358, 5.477, 29.546, 26.134, 28.035
25.806, 5.291, 25.612, 999, 26.495, 26.514, 27.092, 5.385, 4.795, 8.124
1.414, 25.495, 2.449, 26.495, 999, 2.236, 3.162, 30.577, 26.739, 28.635
3.316, 25.278, 4.358, 26.514, 2.236, 999, 1.732, 30.822, 26.645, 28.513
4.472, 25.573, 5.477, 27.092, 3.162, 1.732, 999, 31.416, 26.925, 28.670
29.782, 8.774, 29.546, 0, 30.577, 30.822, 31.416, 999, 7.874, 10.535
26.248, 3.316, 26.134, 4.795, 26.739, 26.645, 26.925, 7.874, 999, 4.358
28.248, 7.348, 28.035, 8.124, 28.635, 28.513, 28.670, 10.535, 4.358, 999];
exc = [1 3 5];
[Amin idc] = min(A(setdiff(1:size(A,1), exc),:),[],2);
[Amin idr] = min(Amin);
idc = idc(idr);
idr = idr+sum(exc<=idr);
fprintf('Minimum %f at row %i col %i\n',Amin,idr,idc);
Minimum 0.000000 at row 8 col 4
I added a zero to test it.
5 Comments
Rakshmy C S
on 13 Dec 2011
Rakshmy C S
on 13 Dec 2011
Andrei Bobrov
on 13 Dec 2011
+1
Andrei Bobrov
on 13 Dec 2011
small corrected
r1 = setdiff(1:size(A,1),exc)
[Amin,idr] = min(A(r1,:))
[Amin,idc] = min(Amin)
idr = r1(idr(idc))
or
r1 = setdiff(1:size(A,1),exc)
[Amin,c1] = min(A(r1,:),[],2)
[Amin,r2] = min(Amin)
idc = c1(r2)
idr = r1(r2)
Rakshmy C S
on 13 Dec 2011
Walter Roberson
on 10 Dec 2011
Create a new matrix that includes only the desired information, and apply the minimum to that.
This can possibly be done without any assignment to variables, by using indexing, but whether you will be able to handle things that way depends on your intention.
B = A; %work on a copy, not the original
B([2 5],:) = []; %remove the 2nd and 5th rows
%now apply the appropriate min() function to B.
or
apply min() to A(setdiff(1:size(A,1), [2 5]),:)
I am being vague about the min because I cannot tell whether you mean minimum across each row with rows 2 and 5 happening not to be wanted; or if you minimum down each column after row 2 and 5 have been ignored; or if you want the minimum over the entire array but excluding the content of rows 2 and 5.
2 Comments
Rakshmy C S
on 10 Dec 2011
Walter Roberson
on 10 Dec 2011
min(min(A(setdiff(1:size(A,1), [2 5]),:)))
Mohsen Davarynejad
on 10 Dec 2011
My guess is the the following scales better:
B = A; %work on a copy, not the original
B([2 5],:) = []; %remove the 2nd and 5th rows
[Min, MinIndex] = min(B(:));
9 Comments
Walter Roberson
on 10 Dec 2011
My recollection is that one of Jan's timing tests showed that min(min(B)) can be faster than min(B(:)) . The working hypothesis is that MATLAB finds it easier to parallelize the min(min()) version.
Mohsen Davarynejad
on 10 Dec 2011
The thing is that when you use min(min()), you have dificuties in returning back the index of the Min element.
Walter Roberson
on 10 Dec 2011
The poster did not ask for the index, just for the minimum.
The index your code creates would be the index in to the B array. If the poster is indeed looking for an index, the poster would likely be looking for an index into the A array, which would be a bit of a nuisance to back-calculate.
Rakshmy C S
on 12 Dec 2011
Sean de Wolski
on 12 Dec 2011
sure it is, just add the number of deleted rows <= to the found index row to the index.
So if you don't want rows [2 5] and the index returned from Walter's code above is
idx = 3; % for this example
bad_rows = [2 5];
idx_original = idx+sum(bad_rows<=idx);
Rakshmy C S
on 12 Dec 2011
Walter Roberson
on 12 Dec 2011
If you got the linear index in to the reduced array, then you have to
[Arow, Acol] = ind2sub(size(A)-[length(badrows) 0], idx) and then do Sean's addition to increase the Arow, to get the row number in the original A. The column number, Acol, will not need to be adjusted.
The linear index shifts by length(badrows) times number of prior columns, plus the number of omitted rows up to the calculated row within this column. Fortunately doing ind2sub() against the reduce matrix takes care of that math transparently.
Rakshmy C S
on 12 Dec 2011
Walter Roberson
on 12 Dec 2011
I see what you mean, and I have an idea of how to correct for it, but I need to work on other things now.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!