How to create a row/column vector of variable length composed of x1,x2,x3,...,xN

I have a column vector, x, of solutions for the equation ax=b, length of x = N, and I want to label which solution is which. N varies depending on the size of the matrix inputted into the function. Want a new vector in the form of xsol = [labels,x] because when you get around 1000 solutions it's not so easy to see what the solutions correspond to. Any help would be greatly appreciated!

3 comentarios

Can you give an example (maybe for just x1,x2) of what inputs and outputs? Ideally, don't just describe them -- use MATLAB syntax to define them.
%%will look something like
x = [-8 ; 2 ; -3 ; 9 ; -5 ; 6];
n = length(x) %%6 in this case.
labels = [x1 ; x2 ; x3 ; x4 ; x5 ; x6];
%%what I want is
xsol = [labels x]
%%the vector labels is what i don't know how to create
Check my answer.

Iniciar sesión para comentar.

 Respuesta aceptada

Birdman
Birdman el 25 de Nov. de 2017
Editada: Birdman el 25 de Nov. de 2017
You may create x vector symbolically by writing
x=sym('x',[N 1]);
Then, solve the equation by writing
x=inv(a)*b;
Hope this helps.

4 comentarios

It does help, sorry i got distracted mid way playing with it! Thank you so much, was about to give up on that vector!
Gonna post this here incase someone else is trying to do similar. When i was running this code i had issues with matlab not simplifying because of the sym command, so i decided to bring vpa into it. I put vpa in bout every spot there is and this is the only spot that made it work correctly.
clear all;
clc;
n=7;
x = [1/3 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7/3];
xlabels = sym('x',[n 1]);
xsol = vpa([xlabels x],5)
And this is the output:
xsol =
[ x1, 0.33333]
[ x2, 2.0]
[ x3, 3.0]
[ x4, 4.0]
[ x5, 5.0]
[ x6, 6.0]
[ x7, 2.3333]
Beautiful, Thanks a ton mate!
Question mate, if I were trying to fprintf that vector, xsol, how would i do that? Haha I'm not good at matlab. I'd like to say:
fprintf('The solution is: %something\n The solution was found in: \n %d iterations', xsol, #ofiterations);
obviously without the number symbol and what-not but with a desired output of:
The solution is:
[x1, 0.33333]
[x2, 2.0]
...
[x7, 2.33333]
This solution was found in 23 iterations.
Birdman
Birdman el 26 de Nov. de 2017
Editada: Birdman el 26 de Nov. de 2017
Check the answer in the following discussion:
Edit: Have you checked the link?
I checked the link and it does the job just fine. Decided to do:
clear all;
clc;
n=7;
i=23;
x = [1/3 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7/3];
xlabels = sym('x',[n 1]);
xsol = vpa([xlabels x],5);
fprintf('The solution is:\n\n')
disp(xsol)
fprintf('The solution was found in %d iterations\n',i)
instead of:
clear all;
clc;
n=7;
i=23;
x = [1/3 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7/3];
xlabels = sym('x',[n 1]);
xsol = vpa([xlabels x],5);
fprintf('The solution is:\n %s\n The solution was found in %d iterations',char(xsol),i)
for cosmetic reasons. It displays the matrix in one row using the char function but using disp outputs it as normal.
Very satisfied! thank you so much!

Iniciar sesión para comentar.

Más respuestas (1)

What is x1, x2, etc.? Do you mean x(1), x(2), etc.????? Otherwise you need to define x1 x2, etc. If it's x(1), x(2), etc. then
labels = x
and xsol is
xsol = [labels, x];
Not sure what good they are, but that's what you asked for.

Etiquetas

Preguntada:

el 25 de Nov. de 2017

Comentada:

el 27 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by