Finding minimum of sum of columns

3 visualizaciones (últimos 30 días)
Chiamaka Agwuegbo
Chiamaka Agwuegbo el 29 de Jul. de 2017
Editada: John BG el 31 de Jul. de 2017
hello guys, thank you for all that you do here.
Please i need help on this code as i have been stuck on this for a while.
This is a simplified version of my code:
a = [2 9 2 8 9 0 1];
b = [3 1 6 2 9 2 5];
c = [0 4 8 7 4 8 3];
d = [a;b;c]
if d(1,1)==d(2,1)||d(1,1)==d(3,1)||d(2,1)==d(3,1) %if at least two of column 1 are the same
return row d that has the minimum sum of columns 4 to 7 (insert code here)
if at least two of the sums of columns 4 to 7 are the same (insert code here);
return row d that has the maximum sum of columns 2 to 3 (insert code here);
if at least two of the sums of columns 2 to 3 are the same (insert code here);
n = d(1,:); %return the first row of d (that is [2 9 2 8 9 0 1])
I would be glad to give more details where needed. Thank you

Respuesta aceptada

John BG
John BG el 30 de Jul. de 2017
Editada: John BG el 31 de Jul. de 2017
Hi Chiamaka
input data
a = [2 9 2 8 9 0 1];
b = [3 1 6 2 9 2 5];
c = [0 4 8 7 4 8 3];
d = [a;b;c];
condition 1.-
if at least two of column 1 are the same
% if d(1,1)==d(2,1)||d(1,1)==d(3,1)||d(2,1)==d(3,1)
if numel(d(:,1))<=numel(unique(d(:,1)))-1
..
end
request 1.-
return row d that has the minimum sum of columns 4 to 7 (insert code here)
sum_d47=(sum(d(:,[4:7])))
s_min=find(sum_d47==min(sum_d47))
R1=d(s_min',:)
sum_d47 =
18
18
22
s_min =
1
2
R1 =
2 9 2 8 9 0 1
3 1 6 2 9 2 5
bear in mind that if you apply condition 1 to any of the requests
all results are void because for the supplied data
numel(d(:,1))==numel(unique(d(:,1)))
=
logical
1
there are not repeated elements in d(:,1)
request 2.-
if at least two of the sums of columns 4 to 7 are the same return row d that has the maximum sum of columns 2 to 3 (insert code here);
sum_d47=(sum(d(:,[4:7]),2))
s_min=find(sum_d47==min(sum_d47))
if numel(s_min)>1
R1=d(s_min',:)
[ai,vi]=max(sum(R1(:,[2 3]),2))
R2=R1(vi,:)
end
sum_d47 =
18
18
22
s_min =
1
2
R1 =
2 9 2 8 9 0 1
3 1 6 2 9 2 5
ai =
11
vi =
1
R2 =
2 9 2 8 9 0 1
request 3.-
if at least two of the sums of columns 2 to 3 are the same (insert code here) n = d(1,:) return the first row of d (that is [2 9 2 8 9 0 1])
sum_d47=(sum(d(:,[4:7]),2))
s_min=find(sum_d47==min(sum_d47))
if numel(s_min)>1
R1=d(s_min',:)
sum_r23=sum(R(:,[2 3]),2)
s_max=find(sum_r23==max(sum_r23))
if numel(s_max)>1
R3=d(1,:)
end
end
sum_d47 =
18
18
22
s_min =
1
2
R1 =
2 9 2 8 9 0 1
3 1 6 2 9 2 5
sum_r23 =
11
7
s_max =
1
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  2 comentarios
Chiamaka Agwuegbo
Chiamaka Agwuegbo el 31 de Jul. de 2017
Hi John. Thanks alot for your answer. I'll be sure to accept it if it works. I really do appreciate the effort
John BG
John BG el 31 de Jul. de 2017
Editada: John BG el 31 de Jul. de 2017
happy to help, please let me know if you reword the question or would like my answer in a function.
John BG

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Jul. de 2017
[~, rowidx] =min( sum(d(:,4:7), 2) );
return_value = d(rowidx,:)
  4 comentarios
John BG
John BG el 30 de Jul. de 2017
sum(A)
and
sum(A,1)
are the same, dimension 1 being sum along vertical.
When aiming at horizontal summation dimension 2 has to be specified.
Chiamaka Agwuegbo
Chiamaka Agwuegbo el 31 de Jul. de 2017
Ok, i see. Thank you

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by