Efficiently finding non zero numbers from a large matrix
问题 I have a 512 x 512 x 112 matrix with three kind of values : zeroes, non zeroes and NaN. How can I get the indices of the non zero values of the matrix efficiently (without using loop)? 回答1: The comment from @scmg is the way to go- Matlab's linear logical indexing is a way to avoid looping over the elements; it only takes 1.2 sec on my PC. Here is a working example: rng(8675309) %jenny number for consistency x=randn([512,512,112]); % make random matrix x(x<0)=NaN; % set some elements to NaN x