Newbie help for matlab

1 view (last 30 days)
Nikola
Nikola on 8 Oct 2014
Commented: Star Strider on 10 Oct 2014
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

Accepted Answer

Star Strider
Star Strider on 8 Oct 2014
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 Comments
Nikola
Nikola on 10 Oct 2014
That is exactly what i was looking for Star Strider, THANKS A LOT !
Star Strider
Star Strider on 10 Oct 2014
My pleasure!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Oct 2014
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 Comments
Nikola
Nikola on 8 Oct 2014
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 on 8 Oct 2014
Edited: Image Analyst on 8 Oct 2014
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, :)]

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!