COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Assigning value to symbolic expressions in vector

• Email
• 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.

```
 0

See related articles to this posting

```"Aquila Stevens"
> but I want
> s1 = 1;
> s2 = 2;
> etc.
>

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

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

us
```
 0

```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

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

```"us " <us@neurol.unizh.ch> wrote in message <hqq355\$cv1\$1@fred.mathworks.com>...
> "Aquila Stevens"
> > but I want
> > s1 = 1;
> > s2 = 2;
> > etc.
> >
>
> 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

```"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
>
> 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