MATLAB Answers

# How to make specific elements in a 3D matrix zero?

8 views (last 30 days)
Rabia Zulfiqar on 22 May 2020
Commented: Rabia Zulfiqar on 23 May 2020
Hi I have a 3D matrix namely Costyearlydata having size of 24x365x10 and I want to make certain elements in that matrix zero.
D has a size of 4000x1x10 and it represents the index of those elements which should be zero.
I am trying with this code but the problem is it only gives correct results for the 1st layer in cost1 matrix and for the remaining nine layers it doesn't make that element zero .What is wrong with my code.Kindly point out my mistake.
for yy=1:10
cost1(:,:,yy)=Costyearlydata(:,:,yy);
cost1(D)=0;
end
Your assistance is much appreciated.I am not so much familiar to dealing with 3D matrix and I am quite new to MATLAB:(
##### 3 CommentsShowHide 2 older comments
Rabia Zulfiqar on 22 May 2020
I also tried this but this one is also giving incorrect answer.
for yy=1:10
cost1(:,:,yy)=Costyearlydata(:,:,yy);
cost1(D(:,:,yy))=0;
end
I don't know how to fix this issue:(

Sign in to comment.

### Accepted Answer

David Goodmanson on 23 May 2020
Hi Rabia, try
for yy=1:10
temp = Costyearlydata(:,:,yy);
temp(D(:,1,yy)) = 0;
cost1(:,:,yy) = temp;
end
##### 1 CommentShowHide None
Rabia Zulfiqar on 23 May 2020
Thanks a ton David:):)

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!