필터 지우기
필터 지우기

Calculate a varibale matrix for a specific value

조회 수: 1 (최근 30일)
Sa Moha
Sa Moha 2021년 12월 16일
댓글: Walter Roberson 2021년 12월 16일
I know this is a beginnary question. But I couldn't find a solution for it.
syms x;
A(x)=[x,x+1,x-1];
A(2)
  댓글 수: 1
Awais Saeed
Awais Saeed 2021년 12월 16일
You did not explain what you are trying to acheive and what is the problem. A(2) gives you [2 3 1] though.

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 16일
syms x;
A(x)=[x,x+1,x-1];
A(2)
ans = 
I suspect that you are trying to index the formula A(x) to extract the x+1 .
If so, then you cannot do that directly.
When you define a symbolic function like you do on the second line, then MATLAB starts with a sym() object and attaches subsref() behaviour for indexing with () indices. The subsref() behaviour that is attached to the symbolic function object is that () indexing works like
extract the variables from the defined parameter list
find the underlying sym() object representation,
subs() into that symbolic representation replacing the
parameter variables with the values that are pass in through the () notation
After that is done, you cannot index into the formula to extract parts of the formula, because the () behavior has been defined as evaluating the formula according to the parameters passed in. Thus A(2) is subs([x,x+1,x-1], {x}, {2}) giving [2 3 1] .
What you can do, though, is
parts = formula(A)
parts = 
parts(2)
ans = 
  댓글 수: 3
Sa Moha
Sa Moha 2021년 12월 16일
I found it myself:
A=zeros(2);
for i=1:2
A(1:2,2*i-1:2*i)=[i,i;i,i];
A
end
Walter Roberson
Walter Roberson 2021년 12월 16일
A = cell(2, 1);
for i=1:2
A{i}=[i,i;i,i];
end
A
A = 2×1 cell array
{2×2 double} {2×2 double}
A{2}
ans = 2×2
2 2 2 2

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

추가 답변 (0개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by