# How to create a vector of symbolic variables with specific labeling

1 visualizzazione (ultimi 30 giorni)
Risposto: John D'Errico il 12 Set 2021
I currently have a vector of unevenly spaced increasing values similar to this one:
v = [1 2 3 5 7 9 10 12 18 29]
What I would like to do is create a vector of symbolic variables such that
vsym = [x1 x2 x3 x5 x7 x9 x10 x12 x18 x29]
I know that I can create a function to produce symbolic variables with increasing indices from 1 to 10 lets say by using sym('x', [1 10]), however I am not able to find a way to quickly assign them different indices as I would desire, such as sym('x', v), whee v would be my vector.
Any help would be appreciated
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Paul il 12 Set 2021
v = [1 2 3 5 7 9 10 12 18 29];
vsym = sym('x',[v(1) v(end)]);
vsym=vsym(v)
vsym =
##### 2 CommentiMostra NessunoNascondi Nessuno
Awais Saeed il 12 Set 2021
Modificato: Awais Saeed il 12 Set 2021
It will only work if vector v starts from 1. If vector v starts from 2, let's say, then the results would be different because the syntax requires rows and columns as sym('x', [rows columns]).
v = [2 3 5 7 9 10 12 18 29];
vsym = sym('x',[v(1) v(end)]);
vsym=vsym(v)
vsym =
Paul il 12 Set 2021
v = [2 3 5 7 9 10 12 18 29];
vsym = sym('x',[1 v(end)]);
vsym = vsym(v)
vsym =

Accedi per commentare.

### Più risposte (2)

Awais Saeed il 12 Set 2021
Not the best way but will work good enough for small vectors
vsym = sym('x',size(v));
for col = 1:1:size(v,2)
char_str = ['x' num2str(v(col))];
vsym(col) = sym(char_str);
end
disp(vsym)
I do not know if this could be done more faster.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

John D'Errico il 12 Set 2021
The simple direct way seems easiest.
v = sym('v',[1 29]);
v = v([1 2 3 5 7 9 10 12 18 29])
v =
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by