# Need help substituting vector variables into symbolic expression

4 visualizzazioni (ultimi 30 giorni)
Jason Bakos il 3 Feb 2024
Commentato: Aquatris il 3 Feb 2024
Hello!
I have a symbolic expression into which I would like to substitute two vector variables:
syms x w1 b1
l1 = tanh(x * w1 + b1);
w1_actual = rand(1,10);
temp = subs(l1,w1,w1_actual);
This correctly generates a 1x10 symbolic array, but when performing the second substitution:
b1_actual = zeros(1,10);
l1_sym = subs(temp,b1,b1_actual);
This generates a 1x100 symbolic array because the 1x10 vector is expanded in each element.
I want the result of both substitutions to yield a 1x10 array, as is the case for the expression:
l1 = x * w1_actual + b1_actual;
I tried making both substitutions simultaneously:
l1 = subs(l1,[w1,b1],[w1_actual,b1_actual]);
but this resulted in the error:
Inconsistency between sizes of second and third arguments.
I also tried declaring the symbols as symmatrices:
syms x [1 1] matrix
syms w1 [1 10] matrix
syms b1 [1 10] matrix
But this prevents subs() from performing any substitutions for either the w1 and b1 variables.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Aquatris il 3 Feb 2024
Seems to work fine however when substituting matrices, you need to clarify where a matrix starts and ends, which is done via cell arrays
syms x w1 b1
l1 = tanh(x * w1 + b1);
w1_actual = rand(1,10);
b1_actual = zeros(1,10);
temp = subs(l1,[w1 b1],{w1_actual, b1_actual});
size(temp)
ans = 1×2
1 10

Accedi per commentare.

### Risposte (3)

Paul il 3 Feb 2024
Modificato: Paul il 3 Feb 2024
syms x y
Option 1. Use a symbolic function
z(x,y) = x + y
z(x, y) =
z([1 2],[100 200])
ans =
Option 2. Symbolic expression, subs with a cell array
z = x + y;
subs(z,{x y},{[1 2],[100 200]})
ans =
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Torsten il 3 Feb 2024
syms x w1 b1
l1 = tanh(x * w1 + b1);
b1_actual = zeros(1,10);
w1_actual = rand(1,10);
l1_actual = arrayfun(@(i)subs(l1,[w1,b1],[w1_actual(i),b1_actual(i)]),1:10)
l1_actual =
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Star Strider il 3 Feb 2024
Modificato: Star Strider il 3 Feb 2024
I may be missing something in your problem statement.
Is this what you want to do —
syms x w1 b1
l1 = tanh(x * w1 + b1);
w1_actual = randi(9,1,10)
w1_actual = 1×10
4 7 9 3 9 1 9 9 2 1
b1_actual = randi(9,1,10)
b1_actual = 1×10
1 4 4 9 6 2 5 8 3 8
temp = subs(l1,{w1,b1},{w1_actual,b1_actual})
temp =
.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Conversion Between Symbolic and Numeric in Help Center e File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by