Max value of row in matrix and creating new matrix

I want something like this matrixa=[12 33 44;26 59 14;15 99 54] matrixb=[26 89 95;23 54 12;2 54 43]
and to get output like this
matrixc=[15 99 54;26 89 95]
calculate the sum of rows in both matrix and to create a new matrix with the two rows that have the greatest sum value like example above. can it be done with max function ? thanks in advance :)

2 件のコメント

José-Luis
José-Luis 2014 年 10 月 7 日
What have you tried so far?
Etan Cole
Etan Cole 2014 年 10 月 7 日
clear; a=[15 9 7;8 5 5;1 7 4]; b=[6 5 9;8 2 5;5 4 4]; [C,I]=max(sum(a,1)); [C1,I1]=max(sum(b,1)); matrix=[a(I,:);b(I1,:)];

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2014 年 10 月 7 日

0 投票

This works:
sa = sum(matrixa,2);
sb = sum(matrixb,2);
c = zeros(size(matrixa));
q1 = matrixa(sa > sb,:);
q2 = matrixb(sb > sa,:);
c(sa > sb,:) = matrixa(sa > sb,:);
c(sb > sa,:) = matrixb(sb > sa,:);

3 件のコメント

Etan Cole
Etan Cole 2014 年 10 月 7 日
clear; matrixa=[15 9 7;8 5 5;1 7 4]; matrixb=[6 5 9;8 2 5;5 4 24]; sa = sum(matrixa,2); sb = sum(matrixb,2); c = zeros(size(matrixa)); q1 = matrixa(sa > sb,:); q2 = matrixb(sb > sa,:); c(sa > sb,:) = matrixa(sa > sb,:); c(sb > sa,:) = matrixb(sb > sa,:);
c=[15,9,7;8,5,5;5,4,24]
why the second row from first matrix i need output matrix 2x3
Star Strider
Star Strider 2014 年 10 月 7 日
Misread it at first. This works:
sa = sum(matrixa,2);
sb = sum(matrixb,2);
mab = [matrixa; matrixb];
sab = [sa; sb];
[srtab,ix] = sort(sab,'descend');
matrixc = mab(ix(1:2),:);
Star Strider
Star Strider 2014 年 10 月 8 日
With respect to your solution, what happens if the two largest row sums happen to be in the same matrix?
My code accounts for that, so that ‘c’ contains the rows of either matrix with the largest row sums.
Your code selects the largest row sums from ‘matrixa’ and ‘matrixb’. They may not be the largest row sums of the two matrices.

サインインしてコメントする。

Etan Cole
Etan Cole 2014 年 10 月 7 日

0 投票

i got it working like this. thanks for the help
[C,I]=max(sum(a,2)); [C1,I1]=max(sum(b,2)); rezultat=[a(I,:);b(I1,:)];

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2014 年 10 月 7 日

コメント済み:

2014 年 10 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by