Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Anyone who can help a Matlab program that perform the following matrix problem

1 回表示 (過去 30 日間)
Keyre
Keyre 2018 年 3 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have n by m matrix.I want to find the maximum values for each columns starting from the first column to the last column. However, there is a restriction that only two maximum values are allowed in each rows.
  1 件のコメント
David Fletcher
David Fletcher 2018 年 3 月 2 日
I'm not really sure I fully understand your intended aim. An example may be useful to clarify the true nature of the problem.

回答 (1 件)

John BG
John BG 2018 年 3 月 2 日
Hi Keyre
1.
Let A be your matrix, for instance
m=5;n=8;A=randi([-10 10],n,m)
A
=
7 10 -2 4 -5
9 10 9 5 -10
-8 -7 6 5 -8
9 10 10 -2 7
3 10 3 3 4
-8 0 -10 -7 -4
-5 6 7 4 9
1 -8 9 -10 -10
2.
Combining command sort and flip the columns are arranged top to bottom, max of each column on each top.
B=flip(sort(A))
=
9 10 10 5 9
9 10 9 5 7
7 10 9 4 4
3 10 7 4 -4
1 6 6 3 -5
-5 0 3 -2 -8
-8 -7 -2 -7 -10
-8 -8 -10 -10 -10
3.
Now let's say you want the 3 max elements of each column.
Simply indexing the following way:
B([1:3],:)
=
9 10 10 5 9
9 10 9 5 7
7 10 9 4 4
You get the 3 largest elements of each column
Keyre
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
  3 件のコメント
John BG
John BG 2018 年 3 月 7 日
Hi Keyre
I chose 3, to show that one can choose any amount of top values (=< amount lines, indeed).
Do you mean this?
trim_top=2;
B([1:trim_top],:)
=
9 10 10 5 9
9 10 9 5 7
Jan
Jan 2018 年 3 月 8 日
@Keyre: If this solves your needs, a hint for a good programming practice: The square brackets waste time here:
B([1:trim_top], :)
This is slightly faster:
B(1:trim_top, :)
[] is the Matlab operator for concatenation, but 1:trim_top is a vector already and there is noting to concatenate. There are further benefits, see Answers: Why not use square brackets.

Community Treasure Hunt

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

Start Hunting!

Translated by