How can I use a nested for loop to find multiple minimum values in a matrix array ?

조회 수: 5 (최근 30일)
I am looking to write a script that will determine what the minimum value in a matrix is, and where it is located
For example the loop will iterate and find the minimum from the last column then assigns the rows which had the elements or numbers on the previous columns to zeros
then it goes and find the minimum again for the last column but this time it will exclude all rows that had zeros
and so on again
GGG=[ 1 4 7 1547
1 4 8 1745.8
1 4 9 2081.4
1 5 7 2099.3
1 5 8 2292
1 5 9 2631.3
1 6 7 2385.7
1 6 8 2557.5
1 6 9 2900.6
2 4 7 1510.3
2 4 8 1712
2 4 9 2037.2
2 5 7 2096.1
2 5 8 2291.7
2 5 9 2620.7
2 6 7 2381.2
2 6 8 2555.9
2 6 9 2888.6
3 4 7 1518.8
3 4 8 1709.5
3 4 9 2065.6
3 5 7 2027.6
3 5 8 2212.2
3 5 9 2572
3 6 7 2314.6
3 6 8 2478.4
3 6 9 2842];
on the above matrix the minimum is on the 9th row the value is 1510.3 2 4 7 1510.3
it will replace all the elements with 2 or 4 or 7 with zeros then check the minum for the resulting matrix
1 5 8 2292
1 5 9 2631.3
1 6 8 2557.5
1 6 9 2900.6
3 5 8 2212.2
3 5 9 2572
3 6 8 2478.4
3 6 9 2842}
then check the minimum again replace with zeors then again till stops print the results
Im really confused on how to write a code for this is what I accomplished so far
if true
clear,clc
GGG=[ 1 4 7 1547
1 4 8 1745.8
1 4 9 2081.4
1 5 7 2099.3
1 5 8 2292
1 5 9 2631.3
1 6 7 2385.7
1 6 8 2557.5
1 6 9 2900.6
2 4 7 1510.3
2 4 8 1712
2 4 9 2037.2
2 5 7 2096.1
2 5 8 2291.7
2 5 9 2620.7
2 6 7 2381.2
2 6 8 2555.9
2 6 9 2888.6
3 4 7 1518.8
3 4 8 1709.5
3 4 9 2065.6
3 5 7 2027.6
3 5 8 2212.2
3 5 9 2572
3 6 7 2314.6
3 6 8 2478.4
3 6 9 2842];
dd=[
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
2 4 8
2 4 9
2 5 7
2 5 8
2 5 9
2 6 7
2 6 8
2 6 9
3 4 7
3 4 8
3 4 9
3 5 7
3 5 8
3 5 9
3 6 7
3 6 8
3 6 9
];
A=GGG;F=dd;bb=4;
for ii=1:bb-1
for m=1:length(A(1,:))
S(m)=A(1,m);
F(F==S(m))=0;
NN=[F A(1:length(GGG),bb)];
% NN(any(NN==0,2),:)=[]
NN(any(NN==0,2),:)=0
for k=2:length(A(:,1));
if(S(m)>A(k,m))
S(m)=A(k,m)
end
end
end
ii
s=ones(bb-1,1)*S
end
s
S
end

답변 (2개)

dpb
dpb 2016년 11월 19일
What is the end result supposed to be??? It appears in the end you could just replace GGG(:,1:3)=0; and be done??? Or is this location of the minimum row the desired result? If so, is it the row in the original array or the reduced one that's significant?
Anyway, the rough outline could be something like--
[mn(1),ix(1)]=min(GGG(:,4)); % first min, location (btw, this is 10 for the example, not 9)
v=GGG(ix(1),1:3); % the values in the row
la=GGG(ismember(GGG(:,1:3),v))=0; % zero 'em...
la=;
[mn(2),ix(2)]=min(GGG(any(GGG(:,3),2),4)); % second step; work only on those nonzero still
NB: The above location is relative to the remaining subset; if you want the original location, then
ix(2)=find(GGG(:,4)==mn(2));
Then follow the above steps to find/clear the columns 1:3, rinse and repeat.
Writing the above in an iterative loop shouldn't be difficult translation.
  댓글 수: 1
Tamir Suliman
Tamir Suliman 2016년 11월 19일
thanks so much for your help the end result is to plot the triangles .The next triangle with minimum perimeter is 358. Since triangles 2,4,7,3,5,and 8 have been used, and can't be reused, the last triangle is 169. So the solution to this problem is a set of 3 triangles 247, 358, and 169.

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2016년 11월 19일

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by