minimum of an array

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

r = [1 3 5];
A1 = A;
A1(r,:) = nan;
[c,idx] = min(A1(:));
[i1,j1] = ind2sub(size(A1),idx);

More Answers (3)

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
Rakshmy C S on 13 Dec 2011
when i applied i got this answer.Array name is dist_dup1.
exc=[1 3 5];
>> [Amin idc] = min(dist_dup1(setdiff(1:size(dist_dup1,1), exc),:),[],2);
>> [Amin idr] = min(Amin);
>> idc = idc(idr);
>> idr = idr+sum(exc<=idr);
>> Amin
Amin =
31.4166
>> idc
idc =
8
>> idr
idr =
6
Rakshmy  C S
Rakshmy C S on 13 Dec 2011
sorry it was my mistake.array name was different. now i got the min value, but the col index is not correct.
minval=1.7321
idr=5
idc=7
idc should be 6
+1
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
Rakshmy C S on 13 Dec 2011
Thank you. this code is also working

Sign in to comment.

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
Rakshmy C S on 10 Dec 2011
i want to find the minimum over the entire array but excluding the content of row2 and 5
min(min(A(setdiff(1:size(A,1), [2 5]),:)))

Sign in to comment.

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

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.
The thing is that when you use min(min()), you have dificuties in returning back the index of the Min element.
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
Rakshmy C S on 12 Dec 2011
i need to find the index of the original array too.so its not possible to copy to newwarray and find the index.
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
Rakshmy C S on 12 Dec 2011
applying walters code i got the minvalue but the index is not correct.After applying your code also i am getting wrong index.
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
Rakshmy C S on 12 Dec 2011
Sorry its not working, i am having the values as given below.It is a 10*10 array. I want to find the minimum value of the elements of array excluding [1,3,5].So the value is 1.7321. and the index is 6,7 or 7,6. But after executing the above code i get 6,9 as the index.
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, 5.385, 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
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!