Finding minimum of sum of columns

3 views (last 30 days)
Chiamaka Agwuegbo
Chiamaka Agwuegbo on 29 Jul 2017
Edited: John BG on 31 Jul 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

Accepted Answer

John BG
John BG on 30 Jul 2017
Edited: John BG on 31 Jul 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 Comments
Chiamaka Agwuegbo
Chiamaka Agwuegbo on 31 Jul 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 on 31 Jul 2017
Edited: John BG on 31 Jul 2017
happy to help, please let me know if you reword the question or would like my answer in a function.
John BG

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 29 Jul 2017
[~, rowidx] =min( sum(d(:,4:7), 2) );
return_value = d(rowidx,:)
  4 Comments
John BG
John BG on 30 Jul 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.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!