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

 Respuesta aceptada

Star Strider
Star Strider el 8 de Oct. de 2014

0 votos

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 comentarios

Image Analyst
Image Analyst el 8 de Oct. de 2014
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 el 10 de Oct. de 2014
That is exactly what i was looking for Star Strider, THANKS A LOT !
Star Strider
Star Strider el 10 de Oct. de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 8 de Oct. de 2014

0 votos

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 comentarios

Nikola
Nikola el 8 de Oct. de 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 el 8 de Oct. de 2014
Editada: Image Analyst el 8 de Oct. de 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, :)]

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 8 de Oct. de 2014

Comentada:

el 10 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by