How to find the indices of non-zero elements in a matrix
Mostrar comentarios más antiguos
Hello,
I have an Na-by-Nt matrix which is sparse, i.e.: most elements are zeros. I want to find the indices of the non-zeros elements in the form of (i,j) where i is the row and j is the column.
I have then these two Na-by-1 vector a and Nt-by-1 vector tau. i corresponds to the ith element in a and j the jth element in tau, and I want to find them as well.
How can I do the above in an efficient way without the need for for loops?
Thanks
Respuestas (1)
If it is for storing only non-zero elements, MATLAB supports SPARSE matrices. If you need to get row/column indices of non-zero elements of A:
[rId, cId] = find( A ) ;
and if you need values as well:
[rId, cId, val] = find( A ) ;
If you just need values, you can get them simply with
vals = A(A ~= 0) ;
11 comentarios
You will end up roughly re-implementing the internal implementation of sparse matrices if you do this, but for what purpose?
S. David
el 27 de Jun. de 2014
For data storage/manipulation of matrices which are sparse enough, I suggest that you use MATLAB built-in sparse matrices, and that you avoid building your own, less efficient and less versatile data structure. Most functions from the MATLAB base package do support sparse matrices and will run algorithms optimized for them when they are given sparse matrices as input.
I recommend that you build sparse matrices using calls of the form
S = sparse(i,j,s,m,n) ;
and that you avoid building them dense first and then converting them to sparse with
S = sparse( A )
At this point, I don't understand why you need indices if you want to get all non-zero values, as you can get them with
val = A(A ~= 0) ;
If you have a vector of row indices and a vector of column indices (which are subsets of the sets of all indices), you can get corresponding values by converting the "subscript" indices to linear indices using SUB2IND, and indexing the array with the linear index.
S. David
el 27 de Jun. de 2014
This implies that I should have one and only one non-zero element in each row of xMtx, and these non-zero elements occur at different column indices.
But you haven't explained what changes to xMtx you are free to make in order to satisfy this property. Do you want to just discard non-zero elements, replacing them with zeros, until xMtx has the pattern of non-zeros that you want?
Incidentally, the property you describe is impossible if Na>Nt.
S. David
el 27 de Jun. de 2014
Cedric
el 28 de Jun. de 2014
Could you build a small numeric example?
S. David
el 30 de Jun. de 2014
I meant a simple/minimal numeric example showing which triplets you need to ID in which data structure, because I am not sure that anybody will have time to understand your code/algorithm.
If it is working but too slow, the first thing that comes to my mind is to replace the last double FOR loop with
A = complex( zeros( numel(s), Np * Ntau )) ;
colId = 0 ;
for pp=1:Np
theta_p=fc.*kron(1+Doppler_Tentative_Set(pp),ones(N))-fr;
Gamma_p=exp(1i*pi*T.*theta_p).*sinc(T.*theta_p);
for tt=1:Ntau
Lambda_p_diag = exp(-1i*2*pi.*(f0+(0:N-1)/T).*Tau_Tentative_Set(tt));
colId = colId + 1 ;
A(:,colId) = bsxfun( @mtimes, Gamma_p, Lambda_p_diag ) * s ;
end
end
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!