필터 지우기
필터 지우기

How to iterate diagonal with alternating values?

조회 수: 3 (최근 30일)
Karen Smith
Karen Smith 2022년 1월 2일
댓글: Matt J 2022년 1월 2일
Hi everyone! I have a question about iterating diagonally across a matrix I create. Here is the code I have so far. I am a beginner at Matlab, so I am trying to do this with a for loop, but getting stuck! If anyone can please help me do this it would be greatly appreciated! I am trying to get the values in green to be set to the value of e_p, thank you so much !!!
N1=2;
N2=4;
nband=1;
n=(N1*N2)*2*nband;
e=1;
e_p=2;
e_pp=3;
t=4;
tp=5;
HM = zeros(n);
for ii=2:n
HM(ii,ii) = e;
end

답변 (1개)

Matt J
Matt J 2022년 1월 2일
One way,
d=ones(1,n);
d(1)=0;
d([2:4:end, 3:4:end])=e_p;
HM=diag(d);
  댓글 수: 2
Karen Smith
Karen Smith 2022년 1월 2일
Hi! Thank you, is it possible to do this with a for loop? I have other for loops in my code that won't run with this line in it
Matt J
Matt J 2022년 1월 2일
HM = zeros(n);
for ii=1:n
HM(ii,ii) = d(ii);
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by