Assigning value to symbolic expressions in vector

  • Follow


Hello,

I have a vector witch contains several symbolic expressions, and another vector witch contains several expressions or values. Example:
syms s1, s2, s3, ...;
X = [s1 s2 s3 ...];
R = [1 2 3 ...];

I want to assign element by element the expressions or values from R to the syms in X, how can I do this?

X(1) = R(1);
Doesn't work, it gives 
X = [1 s2 s3 ...]
but I want
s1 = 1;
s2 = 2;
etc.

Thanks in advance
0
Reply Aquila 4/22/2010 5:46:25 PM

"Aquila Stevens"
> but I want
> s1 = 1;
> s2 = 2;
> etc.
> 
> Thanks in advance

one of the solutions
- to get exactly what you want(?)...

     s1=1;
     s2=2;
     disp(s1)
%    1

us
0
Reply us 4/22/2010 6:05:25 PM


Aquila Stevens wrote:
> Hello,
> 
> I have a vector witch contains several symbolic expressions, and another 
> vector witch contains several expressions or values. Example:
> syms s1, s2, s3, ...;
> X = [s1 s2 s3 ...];
> R = [1 2 3 ...];
> 
> I want to assign element by element the expressions or values from R to 
> the syms in X, how can I do this?
> 
> X(1) = R(1);
> Doesn't work, it gives X = [1 s2 s3 ...]
> but I want
> s1 = 1;
> s2 = 2;
> etc.

I haven't used with the symbolic toolbox as implemented in Matlab, but I have 
read several postings about it.

I believe that what you want is something like:

subs(Expression, num2cell(X), num2cell(R))

Written in long form, this would look like

subs(Expression, {s1, s2, s3, ...}, {1, 2, 3, ...})


However, as I do not have the toolbox available here, I cannot be sure that 
num2cell will work on vectors containing sym entries. I do recall using it 
before in at least one case where the array entries were actually objects 
instead of numbers, so I think it just might work.

The Expression I show here is the expression that you wish the reassignment to 
be evaluated in -- that is, the code here will not actually set s1 to 1, but 
will create a mapping from s1 to 1 within the expression you give.


Other than that... hmmm, let me see:

Rs = arrayfun(@sym, R, 'Uniform', 0);
Xs = arrayfun(@(v) v, 'Uniform', 0);
[Xs{:}] = deal(Rs{:});

That code -would- assign sym(1) to s1, sym(2) to s2, and so on.
0
Reply Walter 4/22/2010 6:07:55 PM

"us " <us@neurol.unizh.ch> wrote in message <hqq355$cv1$1@fred.mathworks.com>...
> "Aquila Stevens"
> > but I want
> > s1 = 1;
> > s2 = 2;
> > etc.
> > 
> > Thanks in advance
> 
> one of the solutions
> - to get exactly what you want(?)...
> 
>      s1=1;
>      s2=2;
>      disp(s1)
> %    1
> 
> us

This will work indeed, but if you have a long list, how could you automate that? I know you could possibly use the eval function, but that is not a good solution. Also, not all symbolic expressions in the vector are named this way. This is why they were put in a vector. I was hoping I could use some sort of pointer or reference.
X(1) = R(1); just replaces the first element of X by the element of R, I want to assign the value of the element of R to the element of X.
0
Reply Geert 4/22/2010 6:47:08 PM

"Walter Roberson" <roberson@hushmail.com> wrote in message 
news:hqq39t$lhd$1@canopus.cc.umanitoba.ca...
> Aquila Stevens wrote:
>> Hello,
>>
>> I have a vector witch contains several symbolic expressions, and another 
>> vector witch contains several expressions or values. Example:
>> syms s1, s2, s3, ...;
>> X = [s1 s2 s3 ...];
>> R = [1 2 3 ...];
>>
>> I want to assign element by element the expressions or values from R to 
>> the syms in X, how can I do this?
>>
>> X(1) = R(1);
>> Doesn't work, it gives X = [1 s2 s3 ...]
>> but I want
>> s1 = 1;
>> s2 = 2;
>> etc.
>
> I haven't used with the symbolic toolbox as implemented in Matlab, but I 
> have read several postings about it.
>
> I believe that what you want is something like:

*snip*

The way I interpreted it was the standard "Create variables x1, x2, x3, 
etc." FAQ.

Aquila, see Q4.6 in the newsgroup FAQ if what you're trying to do is in fact 
create symbolic variables s1 [with the symbolic representation of R(1)], s2 
[with sym(R(2))], etc.  That is strongly discouraged for reasons described 
in that FAQ entry.

If you're instead trying to substitute values into an expression for those 
symbolic variables, then use Walter's approach.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 4/22/2010 6:53:41 PM

4 Replies
302 Views

(page loaded in 0.035 seconds)











5/20/2013 5:17:57 PM


Reply: