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

13 views (last 30 days)
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 Comments
Timothy Corley
Timothy Corley on 25 Nov 2017
%%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

Sign in to comment.

Accepted Answer

Birdman
Birdman on 25 Nov 2017
Edited: Birdman on 25 Nov 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 Comments
Timothy Corley
Timothy Corley on 27 Nov 2017
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!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 26 Nov 2017
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.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!