MATLAB Answers

PAK
0

Problems with indexing into an array with nan (indexing on 2 variables)

Asked by PAK
on 23 Aug 2018
Latest activity Commented on by PAK
on 30 Aug 2018
Hi All,
I'm having trouble using a logical variable ( Bandphase_logical = 9x5272240 logical) to find "true" events in another variable ( bandphase = 3x5272240 single). I think part of my problem is that the "bandphase" variable contains NaNs and I am trying to index over 2 different variables (see below).
"Bandphase_logical" contains 1's where an event occurs across a time series (5272240 samples). "bandphase" contains radian values across the entire timeseries (5272240 samples). Each row contains different frequency bands, and has a "NaN" where that sample does that have a frequency.
I'd like to do something along the lines of (I'd to create a loop where the bandphase variable changes adaptively with how many rows it has and same with the Bandphase_logical variable [e.g.: if bandphase had 5 rows and Bandphase_logical had 12 rows]):
bandphase(1,:)(Bandphase_logical(1,:)
bandphase(1,:)(Bandphase_logical(2,:)
.
.
bandphase(1,:)(Bandphase_logical(9,:)
bandphase(2,:)(Bandphase_logical(1,:)
bandphase(2,:)(Bandphase_logical(2,:)
.
.
bandphase(2,:)(Bandphase_logical(9,:)
bandphase(3,:)(Bandphase_logical(1,:)
bandphase(3,:)(Bandphase_logical(2,:)
.
.
bandphase(3,:)(Bandphase_logical(9,:)
I'd greatly appreciate any help! I'm thoroughly confused.
PK

  0 Comments

Sign in to comment.

Tags

Products


Release

R2018a

1 Answer

Answer by Stephen Cobeldick on 23 Aug 2018
Edited by Stephen Cobeldick on 23 Aug 2018
 Accepted Answer

for ii = 1:size(bandphase,1)
for jj = 1:size(Bandphase_logical,1)
tmp = bandphase(ii,:);
tmp(Bandphase_logical(jj,:))
end
end
To store the output (of different sizes) use a cell array:
S = [size(bandphase,1),size(Bandphase_logical,1)];
C = cell(S);
for ii = 1:S(1)
for jj = 1:S(2)
tmp = bandphase(ii,:);
C{ii,jj} = tmp(Bandphase_logical(jj,:))
end
end
You can download padcat to automatically pad with NaN's and create one numeric array:
mat = padcat(C{:})

  5 Comments

PAK's "Answer" moved here:
Hi Stephen and Walter,
This does work! I created a variable to store "tmp(Bandphase_logical(jj,:)). However, how do I make that variable ("phases") not overwrite with each iteration of jj (i.e.: so there are jj rows in "phases")? Each row of "phases" will be a different column length (e.g.: for jj = 1 it returns a 1x24 vector, for jj = 2 it returns 1x13 vector). I can't just do below, because the vectors are of different lengths.
phases(jj) = tmp(Bandphase_logical(jj,:))
Is there a way to place NaN to make the vector lengths the same as the matrix is getting populated?
Thank you again!
PK
This was amazing. Thank you!!

Sign in to comment.