필터 지우기
필터 지우기

Question on creating dynamic matrix variables

조회 수: 2 (최근 30일)
Tsz Tsun
Tsz Tsun 2024년 4월 12일
댓글: Tsz Tsun 2024년 4월 12일
Hi all, I would like to know how to write dynamic matrix variables. For example, I have the following:
clear all;
n=11
n = 11
for i=1:n-1
A1(i,i+1) = 1;
A1(i+1,i) = 1;
end
A1
A1 = 11x11
0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i=1:n-2
A2(i,i+2) = 1;
A2(i+2,i) = 1;
end
A2
A2 = 11x11
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i=1:n-3
A3(i,i+3) = 1;
A3(i+3,i) = 1;
end
A3
A3 = 11x11
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And I would like to make something like this:
for k=1:1:3
for i=1:n-1
A[k](i,i+1) = 1;
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
A[k](i+1,i) = 1;
end
end
Or alternatively, is there a neat way to do so?

채택된 답변

Stephen23
Stephen23 2024년 4월 12일
편집: Stephen23 2024년 4월 12일
"Or alternatively, is there a neat way to do so?"
Of course there is: indexing. Either into a numeric array or into a container array (e.g. a cell array).
Indexing is neat, simple, and efficient. Unlike what you are trying to do.
n = 11; % size of each matrix
D = 2:4; % vector of indices of the 1's
C = cell(size(D));
for k = 1:numel(D)
V = zeros(1,n);
V(D(k)) = 1;
C{k} = toeplitz(V);
end
Checking:
C{1}
ans = 11x11
0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C{2}
ans = 11x11
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C{3}
ans = 11x11
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 5
Stephen23
Stephen23 2024년 4월 12일
Either modify the TOEPLITZ call with two vector inputs, or call DIAG:
n = 11; % size of each matrix
D = 1:3; % offset of the diagonal
C = cell(size(D));
for k = 1:numel(D)
C{k} = diag(ones(1,n-D(k)),D(k));
end
C{1}
ans = 11x11
0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C{2}
ans = 11x11
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C{3}
ans = 11x11
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Tsz Tsun
Tsz Tsun 2024년 4월 12일
Thank you very much for your help!

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

추가 답변 (1개)

Taylor
Taylor 2024년 4월 12일
May be worth looking into structures instead of matrices. https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by