-3

If I have a matrix like this:

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           ]

I want to find the top 3 max values in every rows in Matlab. what i do in Matlab? and is it true? i want to find top-N method in select neighbors.

sima412
  • 255
  • 2
  • 7
  • 16

2 Answers2

1

I would recommend rewording your question. You say you want the top ten max values in every row, but the matrix you gave has only five columns :/

I think that what you are looking for is something like this.

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.

Hope this answers your question.

dsgrnt
  • 393
  • 2
  • 11
  • oh sorry i update it. thank you. but 2 what is in B = sort(sample,2,'descend') ? and if i have a matrix 100*100 then is it true? – sima412 Aug 23 '13 at 19:27
  • Yes. I am simply sorting that rows in the array called `sample` and collecting the results in a variable called `B`. Then I am picking off the top `N` values by indexing the columns with `B(:,1:N)`. For example, if I wanted the top ten values in the first row of `sample` I would do the following: `B = sort(sample,2,'descend'); top10row1 = B(1,1:10);` Hope that makes it clear to you. – dsgrnt Aug 23 '13 at 20:04
  • The `2` in `sort(sample,2,'descend')` simply means that we are sorting along the second dimension of the array `sample`. Try typing `help sort` at the command prompt. The `help` function is your friend. – dsgrnt Aug 23 '13 at 20:07
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.

Frederick
  • 1,271
  • 1
  • 10
  • 29
  • @Ferdrick sorry i don't understand what you answer. can you Explain more?desired_dimension what is? thank you – sima412 Aug 23 '13 at 18:51
  • If I understand your question correctly, then I cannot think of an answer better than @dsgmt's. – Frederick Aug 23 '13 at 19:24
  • thank you very much, yes i think this is true, but do you know "2" what is in B = sort(sample,2,'descend') ? and if i have a matrix 100*100 then what this code use? – sima412 Aug 23 '13 at 19:26
  • `help sort` " Y = sort(X,DIM,MODE) has two optional parameters. DIM selects a dimension along which to sort." It would be the same. – Frederick Aug 23 '13 at 19:29