How to open gaps after uses the "find" command?

1 view (last 30 days)
I am working with a matrix data=[time depth density], and I am trying to select the densities of a specific range of depth. I tried the following expression:
>>dens=data(:,3);
>>ind_10=find(data(:,2)>=10);
>>dens_10=dens(ind_10);
It worked, but the matrix now have a size smaller than the previous one. I would like to keep dens_10 following the same "time", in order to plot then together. In other words, how can I open gaps on the new matrix in other to this one have the same size as the old one?
Thank you very much!

Accepted Answer

Stephen23
Stephen23 on 3 Feb 2015
Edited: Stephen23 on 3 Feb 2015
Although find is very attractive for beginners, it is actually faster and neater to use logical indexing, so the equivalent to your code above would be:
ind_10 = data(:,2) >= 10;
dens_10 = data(ind_10,3);
This also allows us to easily define not only which elements we select from an array, but also those that we allocate into an array:
ind_10 = data(:,2) >= 10;
dens_10(ind_10) = data(ind_10,3);
You will probably need to consider what values to put in the non-allocated positions, something like this:
dens_10(~ind_10) = NaN; % or 0?

More Answers (0)

Categories

Find more on Dictionaries in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!