PLEASE HELP ME find coresponding column indexes.

I have a matrix V of size (3x3) and another matrix W (3x3), where off-diagonals are zero in matrix W and diagonals are non-zero. Then I have A=sort(diag(W)). Now I want to get the corresponding column index number from matrix W.
CODE IS GIVEN BELOW:
V= [3 4 5; 4 7 8;2 6 1];
W=[5 0 0;0 7 0;0 0 2];
A=sort(diag(W));
A =
2
5
7
Now i want the corresponding column index number. for example: in this problem first element is 2 and corresponding column index number 3 from matrix W. Similarly for 5 is 1 and for 7 is 2 respectively.

5 Comments

‘Now i want to get the corresponding column index number from matrix W.’
Corresponding to what?
Please edit your question.
  • Format the code so that it is readable (use the {} Code button above the text box)
  • Describe the problem in more detail (as Star Strider has already pointed out)
  • Don't put three sentences in the title.
  • Read this:
corresponding to sorted elemnt
Do you mean the rows and columns of V that correspond to values in A? Is so, why even bother to sort W? And what if the numbers in W don't appear in V? read this
Interestingly you don't refer to matrix V at all. Is this intentional?

Sign in to comment.

 Accepted Answer

If you read the documentation for the sort function, you will find that there is a second output giving the indices of the sort. Your code would become:
[A,I] = sort(W)
Try that, and it might be useful for solving your problem.

3 Comments

True enough, but sorting a diagonal matrix ... ?
Yes, the whole thing is a bit fishy... hopefully the OP can provide some more information on what they really are trying to achieve.
Thank you so much Stephen

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Asked:

on 4 Jan 2015

Commented:

on 4 Jan 2015

Community Treasure Hunt

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

Start Hunting!