How to select non zero elements of matrix/array and return a 2d array of values and index positions

2 ビュー (過去 30 日間)
The question is how can I identify the non zero elements of a 2D matrix and construct a new sorted 'value , 'index' array.
Context: For the problem I am working on the index position indicates specific linkages between the x, y index values and the non zero value in the matrix is the strength of the link between that index pair. The generated 2d array of value and index is subsequently going to be used for statistical t test analysis.
A simplified example of what I am trying to achieve is shown below. Any assistance much appreciated.
a = [ 1 0 0 0 0; 0 3 0 0 0; 0 0 5 0 0 ; 0 0 0 7 8]
a =
1 0 0 0 0
0 3 0 0 0
0 0 5 0 0
0 0 0 7 8
Desired result is to show the non zero values , sorted ascending and the index position in the data matrix a in this example. How the index position is displayed is not so important , I have just chosen an example format [x,y] to try and make my question clear but I do need the numerical x and y position values for the non zero elements.
Value index
1 [1,1]
3 [2,2]
5 [3,3]
7 [4,4]
8 [4,5]
I've tried solutions such as [b,c] = sort(a,1) but this just gives me new matrices and doesn't pull out the actual index position of the values.

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 6 月 29 日
編集済み: Andrei Bobrov 2015 年 6 月 29 日
[r,c,v] = find(a);
out = [v,r,c];
  1 件のコメント
AndyT
AndyT 2015 年 6 月 29 日
Wow you guys are quick ! and superb !! Thanks this will do it and gives me the option of manipulating the data in a number of ways.

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

その他の回答 (1 件)

Matt J
Matt J 2015 年 6 月 29 日
編集済み: Matt J 2015 年 6 月 29 日
[x,y,val]=find(a);
sortrows([val, x,y],1),

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by