How do I replace/convert all zeros in 3rd-dimension to NaN

Hi All,
I have a 5-D matrix: 5x16x20x3x15, where 5x16 is the data set; 20 is the number of trials (per experiment); 3 is the number of experiments (per participant); and 15 is the number of participants. If I had 1 particant that had a dataset (for only one exp) that was exclusively all zeros i.e. 5x16x20x2x2 was entirely zeros; would there be a way to convert the zeros to NaN, so that my mean and std calculations are not affected?
Cheers.

 Accepted Answer

yourMatrix(:,:,affectedTrials,affectedExperiment,affectedParticipant)=nan;
If all trials of the affected experiment are zeros, then:
yourMatrix(:,:,:,2,2)=nan;

3 Comments

Thanks. Is there a way of incorporating this into an if statement, so that I don't have to manually do this for each experiment per participant. I came up with the following but it's not changing the 0 to NaN:
...
for trl = 1:nTrial
%%load trial data%%
DataCon(:,:,trl,nExp,part) = data(ind1:ind2, ch)*1000;
if sum(data(:,:),[1 2])==0
idx(trl)=NaN;
end
DataCon = DataCon(:,:,idx,nExp,part);
end
...
If your matrix is not too large, this will work fine.
for k=1:size(yourMatrix,3)
for n=1:size(yourMatrix,4)
for m=1:size(yourMatrix,5)
if nnz(yourMatrix(:,:,k,n,m))==0
yourMatrix(:,:,k,n,m)=nan;
end
end
end
end

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Asked:

NA
on 5 Sep 2020

Commented:

NA
on 5 Sep 2020

Community Treasure Hunt

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

Start Hunting!