after command bottom, I want X=[d2;d4;d9]

clc
clear
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s)
end

댓글 수: 10

Geoff Hayes
Geoff Hayes 2014년 10월 28일
tatina - please describe what you are attempting and what you would like to happen. It is not clear from your question or the source code provided. (For example, why are you using syms?)
Reza
Reza 2014년 10월 28일
Matrix X use for Solve Eq
Matt Tearle
Matt Tearle 2014년 10월 28일
What you've posted creates 3 symbolic variables, all 1-by-1. The command X = [d2;d4;d9] would work and create a 3-by-1 symbolic variable X. So you'll need to explain what the problem is. What do you want X to look like? Why is there a problem?
clc
clear
X=zeros(3,1)
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s)
X(i,1)=s
end
But;Error
Geoff Hayes
Geoff Hayes 2014년 10월 28일
Tatina - try formatting your code to keep it distinct from the rest of your question and/or comments. Highlight the code portion and press the {} Code button.
And if you are observing an error, please provide the full error message (all that text which appears in red).
Reza
Reza 2014년 10월 28일
Subscripted assignment dimension mismatch
Geoff Hayes
Geoff Hayes 2014년 10월 28일
Why are you trying to create an array of symbols? How are you planning to use X?
Reza
Reza 2014년 10월 28일
B matrix and C matrix is known , X Matrix is unknown , ===> B=C*X , X=?
James Tursa
James Tursa 2014년 10월 28일
@Geoff: Not sure of Tatina's reason, of course, but I have done this many times myself. E.g., to pass some symbolic matrices & vectors through a section of code to verify the resulting formulas match expected model. Etc.
Geoff Hayes
Geoff Hayes 2014년 10월 29일
@James - thanks for the clarification. I've never used the Symbolic Toolbox so wasn't sure if you this was something that is expected/typical.

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

답변 (1개)

James Tursa
James Tursa 2014년 10월 28일

0 개 추천

Is this what you want? Basically repeated your code, but get rid of the zeros initialization which will make X a double array (not what you want):
clc
clear
% X=zeros(3,1) % Commented out
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s);
X(i,1)=s; % Build up symbolic matrix
end

댓글 수: 3

Reza
Reza 2014년 10월 28일
Error is Subscripted assignment dimension mismatch.
James Tursa
James Tursa 2014년 10월 28일
편집: James Tursa 2014년 10월 28일
Sorry about that. I don't have MATLAB on the machine I am using right now, so can't verify the syntax. Try either X(i) = s, or pre-allocate X=[] and then use X=[X;s]. I know there is a way to do this because I have done it before. Could always build up a char array of the result and then eval it, I suppose.
James Tursa
James Tursa 2014년 10월 29일
편집: James Tursa 2014년 10월 29일
Another attempt to get the symbolic vector defined in your code (still not on a machine w/MATLAB, so this is untested)
clc
clear
y='X=['; % Used to build up the symbolic vector string
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s);
y = [y 'd' int2str(A(i)) ';']; % Build up symbolic string
end
y(end) = ']'; % Replace the last ; with ]
eval(y); % Generate the symbolic vector

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

태그

아직 태그를 입력하지 않았습니다.

질문:

2014년 10월 28일

편집:

2014년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by