Convert the row of a matrix into a vector

Hello everybody.
I have my matrix "A" wich the user define its size, for example, the user define a size 3x5 for the matrix A:
[1 2 3 4 5]
A= [2 4 6 8 3]
[2 4 5 7 8]
I need to convert the rows into new vectors like this:
A1=[1 2 3 4 5]
A2=[2 4 6 8 3]
A3=[2 4 5 7 8]
But the problem is that, as the user can modify the size of the matrix (adding/deleting rows), I need to take the vectors. For example, the user define a new size of the matrix A as 5x5, so I need
A1=[...]
A2=[...]
...
A5=[...]
I would not like to manually put the new vectors because the number of rows may become too large (around 500 or more) and put each new vector manually it becomes almost impossible. So I need that MatLab do this automatically.
Thank You.

Answers (2)

Adding rows to a matrix is easy enough. There are several ways, one of which is the cat function. If you want to append vector ‘V’ to matrix ‘A’ using it, you would define the new ‘A’ as:
A = cat(1, A, V);
Defining the elements of ‘V’ appears to be the problem, however. How do you want to define the new row? Can the elements of it be random, or is there some rule you want to use to define them?

4 Comments

That is not the question. Maybe I didn't do it the right way.
My question is about doing the opposite. I have a matrix and I need the rows, but each row has to be a vector because i need to compute each vector separately.
I have this example code
dim=2; % Define by User
dim1=15*dim;
A=[];
B=[];
x=round(rand(dim1));
for i=1:dim
A(i,1)=x(i);
end
for i=1:dim1
B(i,1)=x(i+1);
end
B=vec2mat(B,15);
I have a matrix B, but I need to take its rows into a separately vectors:
B1= B(1,:)
B2= B(2,:)
The problem is that dim may be 500 or more, and write so many vectors manually its very hard.
I do not have the Communications System Toolbox, so I do not have ‘vec2mat’ and I cannot run your code. I do not understand what you want to do, so I will delete this Answer in a few minutes. I have added the ‘Communications Systems Toolbox’ to the product tags so others know that they need to have it to Answer your Question.
Whatever you are doing, DO NOT DO THIS:
B1= B(1,:)
B2= B(2,:)
Just use a loop, and create your other matrices from vec2mat, saving them as you need them. Use cell arrays if the sizes of the matrices vary. If they are all the same size, you can concatenate them in a multi-dimensional matrix, using the cat function but with the first argument being 3, not 1.
I'm sorry if I have not made my point.
The problem is this:
The user define a matrix, for example:
[1 2 3 4 5]
A= [2 4 6 8 3]
[2 4 5 7 8]
So, the program has to show me the rows in different vectors
A1=[1 2 3 4 5]
A2=[2 4 6 8 3]
A3=[2 4 5 7 8]
If the user gives me other matrix
[1 2 3 4 5]
[2 4 6 8 3]
A= [2 4 5 7 8]
[4 5 6 6 7]
[8 2 3 3 1]
The program has to show me
A1=[1 2 3 4 5]
A2=[2 4 6 8 3]
A3=[2 4 5 7 8]
A4=[4 5 6 6 7]
A5=[8 2 3 3 1]
The range of the rows of the matrix that the user define is between 30 and 1200. So, I need that MatLab make the vectors of each row automatic.
I tried to define each vector this way:
AA = sym('A%d', [rows 1]);
So I have A1, A2, A3, etc... and, after that, I could do (for the last example)
A1=[1 2 3 4 5]
A2=[2 4 6 8 3]
A3=[2 4 5 7 8]
A4=[4 5 6 6 7]
A5=[8 2 3 3 1]
But, I don't know how to make that loop.
I hope I make understand my point correctly
If all you want to do is print them to a file or to the Command Window, that is straightforward:
A = [1 2 3 4 5
2 4 6 8 3
2 4 5 7 8
4 5 6 6 7
8 2 3 3 1];
v = 1:size(A,1); % Define Rows
Rows = sprintf(['A%d = [ ' repmat('%.0f ', 1, size(A,2)) ']\n'], [v' A]')
Rows =
A1 = [ 1 2 3 4 5 ]
A2 = [ 2 4 6 8 3 ]
A3 = [ 2 4 5 7 8 ]
A4 = [ 4 5 6 6 7 ]
A5 = [ 8 2 3 3 1 ]
You don’t even need a loop. This keeps them in your workspace as a string variable, ‘Rows’.
To print them to a file, see the documentation for the fprintf function. The essence of the code remains the same, but you have to define a file and a file identification number for it to print to.

Sign in to comment.

This will display what you asked for:
% Create sample data.
A=[1 2 3 4 5;...
2 4 6 8 3;...
2 4 5 7 8;...
4 5 6 6 7;...
8 2 3 3 1]
% Display the desired rows in the specified format
for row = 1 : size(A, 1)
fprintf('A%d = [', row);
fprintf('%d ', A(row,:));
fprintf(']\n');
end
It does not create 5 or 500 new row vectors - there is no need for that that I can see. Why on earth would you want that? I mean, why would you deliberately want to go against the recommendations in the FAQ? http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Asked:

on 26 Dec 2015

Commented:

on 26 Dec 2015

Community Treasure Hunt

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

Start Hunting!