Jose L Gutierrez A
Jose L Gutierrez A

Reputation: 45

Convert the row of a matrix into a vector

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.

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=5;            % 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 the matrix B, but I need to take its rows into a separately vectors:

B1= B(1,:)
B2= B(2,:)
...
B5=B(5,:)

The problem is that dim may be 500 or more, and write so many vectors manually its very hard. So I need help doing the loop.

---------------- EDIT--------------

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.

Thank You

Upvotes: 1

Views: 122

Answers (1)

Andreas H.
Andreas H.

Reputation: 6073

The only way to assign to variables with different name is to use the command eval or assignin. Use of either is typically indicating bad programming practice (but this is due to your problem statement -- it can only be solved with these two commands). If you really insist on creating individual variables with different names I would prefer the assignin approach here:

for ii=1:size(A,1)
   assign( ['A' num2str(ii)], A(ii,:) );
end

with the following code placed in assign.m file

function assign( varname, val )
assignin('caller', varname, val );

BTW: A (slightly) cleaner solution (which does not rely on eval and assignin) would be to use a struct. i.e.

S = struct;
for ii=1:size(A,1)
   S.(['A' num2str(ii)]) = A(ii,:);
end

which now contains S.A1, S.A2 and so on.

Upvotes: 0

Related Questions