-3

如果我有这样的矩阵:

sample = [1              0.21852382     0.090085552    0.219984954 0.446286385;
          0.21852382     1              0.104580323    0.138429617 0.169216538;
          0.090085552    0.104580323    1              0.237582739 0.105637177;
          0.219984954    0.138429617    0.237582739    1           0.192753169;
          0.446286385    0.169216538    0.105637177    0.192753169 1           ]

我想在 Matlab 的每一行中找到前 3 个最大值。我在 Matlab 中做什么?这是真的吗?我想在选择的邻居中找到前 N 个方法。

4

2 回答 2

1

我建议改写你的问题。你说你想要每一行的前十个最大值,但是你给出的矩阵只有五列:/

认为您正在寻找的是这样的东西。

sample = [1              0.21852382     0.090085552    0.219984954 0.446286385;
          0.21852382     1              0.104580323    0.138429617 0.169216538;
          0.090085552    0.104580323    1              0.237582739 0.105637177;
          0.219984954    0.138429617    0.237582739    1           0.192753169;
          0.446286385    0.169216538    0.105637177    0.192753169 1           ]
B = sort(sample,2,'descend') % will sort the rows of the array in descending order   
C = B(:,1:N)                 % Select the top N values.

希望这能回答你的问题。

于 2013-08-23T16:11:13.987 回答
0

If that isn't what you want, try [Y,I] = max(matrix,[],desired_dimension) where Y and an array of the is the actual max values (e.g. [1 1 1 1 1]) and I is the index of the max values, (e.g [1 2 3 4 5])

EDIT If desired_output = [1 1 1 1 1]', (a column vector, note transpose), then the command to do that is max(matrix,[],2) to operate along the second dimension. This behavior is defined in help max.

于 2013-08-23T16:29:34.967 回答