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

조회 수: 3 (최근 30일)
Rabia Zulfiqar
Rabia Zulfiqar 2020년 5월 22일
댓글: Rabia Zulfiqar 2020년 5월 23일
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
Rabia Zulfiqar
Rabia Zulfiqar 2020년 5월 22일
yes it represents the index relative to 24x365 plane. For example in each layer I have 8760 elements. D is the linear index from 1 to 8760.
D has 10 layers so for each layer it has indexing from 1 to 8760.
Rabia Zulfiqar
Rabia Zulfiqar 2020년 5월 22일
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:(

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 2020년 5월 23일
Hi Rabia, try
for yy=1:10
temp = Costyearlydata(:,:,yy);
temp(D(:,1,yy)) = 0;
cost1(:,:,yy) = temp;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by