very simple logic but im not getting
n=4
K = [ 1, 2; 3, 4]
K1=zeros(n+1);
K1([1 2],[1 2])=K
K2=zeros(n+1);
K2([2 3],[2 3])=K
K3=zeros(n+1);
K3([3 4],[3 4])=K
K4=zeros(n+1);
K4([4 5],[4 5])=K
L=K1+K2+K3+K4;
how to convert this into loop where we can add more Kn matrices

댓글 수: 2

Stephen23
Stephen23 2019년 3월 9일
편집: Stephen23 2019년 3월 9일
"how to convert this into loop where we can add more Kn matrices"
Do NOT do that. Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
Indexing is neat, simple, and very efficient. You should use indexing, e.g. with a cell array or an ND numeric array.
page vnky
page vnky 2019년 3월 9일
thanks stephen!! that gave me lot and i got it.

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

 채택된 답변

Guillaume
Guillaume 2019년 3월 10일

0 개 추천

In addition to what Stephen said, it's very easy to create your L for any n, without even a loop:
K = [1 2; 3 4];
n = 5;
L = toeplitz([trace(K), K(2, 1), zeros(1, n-2)], [trace(K), K(1, 2), zeros(1, n-2)]);
L([1, end]) = K([1, end])

댓글 수: 1

page vnky
page vnky 2019년 3월 10일
wow guillaume, i got one step answer with yours... thanks a lot

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 3월 9일

댓글:

2019년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by