how do i write a function that has two 3x3 input matrices and in output it gives new matrix that contains the rows of the input that has the least sum .I.e. a=[1 2 3;2 3 4; 3 4 5] and b=[1 2 3;2 3 4; 3 4 5] then print c=[1 2 3;1 2 3] . I have the logic in my head but i can't code it in matlab since i have only 1 lesson on it. Thanks

 채택된 답변

Star Strider
Star Strider 2014년 10월 8일

0 개 추천

See if this does what you want:
a=[1 2 3;2 3 4; 3 4 5];
b=[1 2 3;2 3 4; 3 4 5];
sa = sum(a,2);
sb = sum(b,2);
mab = [a; b];
sab = [sa; sb];
[srtab,ix] = sort(sab,'ascend');
c = mab(ix(1:2),:)
producing:
c =
1 2 3
1 2 3

댓글 수: 3

He added a requirement that it must have the min row in a, and the min row in b. So your code would not work in this case:
a=[1, 2, 3; 1, 2, 3; 3, 4, 5];
b=[2, 3, 4; 2, 3, 4; 3, 4, 5];
since it will take both of the 1,2,3 rows from a and no row(s) at all from b. I have a question pending with him about what if the min occurs with two rows that have the same sum but different numbers. Wonder how he'll reply.
Nikola
Nikola 2014년 10월 10일
That is exactly what i was looking for Star Strider, THANKS A LOT !
Star Strider
Star Strider 2014년 10월 10일
My pleasure!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 8일

0 개 추천

Try this:
% Create sample data
a=[1 2 3;2 3 4; 3 4 5]
b=[3 2 1;2 3 4; 2, 2, 2]
% Find the sums over columns for both arrays.
columnSumsA = sum(a, 2)
columnSumsB = sum(b, 2)
% Find the overall minimum sum.
MinSumOverall = min([columnSumsA; columnSumsA])
% Find out where (what row) that sum occurs in matrix a & b.
rowLocationInA = find(columnSumsA == MinSumOverall)
rowLocationInB = find(columnSumsB == MinSumOverall)
% Construct C from those rows extracted out from a and b
c = [a(rowLocationInA,:);, b(rowLocationInB, :)]
In the command window, you can see the individual steps results:
a =
1 2 3
2 3 4
3 4 5
b =
3 2 1
2 3 4
2 2 2
columnSumsA =
6
9
12
columnSumsB =
6
9
6
MinSumOverall =
6
rowLocationInA =
1
rowLocationInB =
1
3
c =
1 2 3
3 2 1
2 2 2

댓글 수: 2

Nikola
Nikola 2014년 10월 8일
that's not i am looking for ,the input is 3x3 but the output is 3x2, one vector from both matrices that has the least sum should be printed in new variable, so c should be c=[vector with least sum from a;vector with least sum with b] ;. I know it is hard for me explain. Also thanks.
Image Analyst
Image Analyst 2014년 10월 8일
편집: Image Analyst 2014년 10월 8일
That's what it would do if the sum occurred only once. But look at my example. You can have a sum of 6 with 1,2,3 or with 2,2,2. Well, what if both of those occur in "b"? Which row(s) do you extract? I extracted both/all. If you wanted to take just first one, or the last one, you could do that. What do you want to do if b looks like this:
b =
3 2 1
2 3 4
2 2 2
such that the min sum occurs on more than 1 row?
[EDIT] To take the first one, do this:
% Create sample data
a=[1 2 3;2 3 4; 3 4 5]
b=[3 2 1;2 3 4; 2, 2, 2]
% Find the sums over columns for both arrays.
columnSumsA = sum(a, 2)
columnSumsB = sum(b, 2)
% Find out where (what row) that sum occurs in matrix a & b.
[minSumA, rowLocationInA] = min(columnSumsA)
[minSumB, rowLocationInB] = min(columnSumsB)
% Construct C from those rows extracted out from a and b
c = [a(rowLocationInA,:);, b(rowLocationInB, :)]

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2014년 10월 8일

댓글:

2014년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by