1

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

  • 3
    *Why* do you need to do this? You can pass rows to whatever function you want using the colon notation, there is no reason to separate them into unique variables. – sco1 Dec 26 '15 at 17:42
  • 3
    Nonononono... Why would you create 500 new variables when `A(1,:)` is perfectly serviceable? And easier to loop over. – beaker Dec 26 '15 at 17:53
  • I edited the question, so I hope I made my point correctly. – Jose L Gutierrez A Dec 26 '15 at 18:32
  • @JoseLGutierrezA your point was not unclear, your reason for needing to do it this way was. What you want to do is generally bad programming practice and there really isn't any need to do it this way. – sco1 Dec 26 '15 at 18:35
  • I agree with others. you haven't given a reason as to why you'd need these vectors in separate variables. each row is available to whatever operation you could want via array indexing. maybe if you explain your vector processing? for instance, say you element wise multiply row 33 with row 55, you can achieve this with A(33, :).*A(55, :) – crowdedComputeeer Dec 26 '15 at 18:45
  • Obviously you're not just printing these rows. What operations do you want to perform on these new variables? – beaker Dec 26 '15 at 18:50

1 Answers1

0

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.

Andreas H.
  • 5,557
  • 23
  • 32