0
A= [1 2 3 4  
    2 3 4 5  
    4 5 6 7  
    .  
    ....]  

where each of the rows is stored in a separate vector as such

a1 = [1 2 3 4]  
a2 = [2 3 4 5]  
.  
.  
.  
an = [1 2 3 4]  

and I need to create new cells, using a loop, containing all previous row vectors as follows:

vectors = {a1, a2, a3, ......,an} 

in the workspace I get vectors as a 1 x n cell and in each cell containing its own vector; e.g. the first cell contains vector a1, the second cell contains vector a2, etc. I don't want to copy the code every time I have a different number of vectors, so I'd like to automate this.

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Emanuel
  • 33
  • 6
  • 5
    Where do you `a1` to `an` vectors come from? Are you generating them in your code? It makes much more sense to alter the code that generates them than to aggregate them after the fact. – Dan May 23 '16 at 14:07
  • 1
    somebody edited my question in first place as now the question is useless as is fairly simple to concat matrices. i need as i put the question first to create an array of vectors with each vector in his own cell as I can do it manually – Emanuel May 23 '16 at 15:43
  • Not really, since the usage of those dynamic variables makes it harder than usual. I read all the revisions to your question and I now see where the confusion stems from; you wanted "a row vector `vectors = {a1,...,an}`. Textually you state "row vector", which is what the original editor changed to; syntactically you want to create a cell. Sorry for rolling back your question, I was confused by the ambiguity. Also: you know you can edit comments instead of posting 4 comments with half a sentence each? – Adriaan May 23 '16 at 15:47
  • vectors = {a1,a2,a3} but i need to add these vectors for an unknown number or vectors and i can't figure out sorry every time i press enter is just creating a new comment – Emanuel May 23 '16 at 15:48
  • 2
    @Sardar_Usama please take more care with your edits, this is at least the second time I have seen you assume the OPs intent and make changes based on this. In this case you [harmed the question](http://stackoverflow.com/revisions/37393178/2) by changing `{}` to `[]`, which is a completely different data type in MATLAB. If you don't understand the language, do not edit the code. Also shame on [the reviewers](http://stackoverflow.com/review/suggested-edits/12445836) for approving... – sco1 May 23 '16 at 15:51
  • I wanted to make it easier to put my data in the vectors array instead of typing all the the time v = {a, b, c, ....} – Emanuel May 23 '16 at 16:03
  • I put a new question explaining better off. – Emanuel May 23 '16 at 16:23
  • 1
    @Sardar_Usama that just means that reviewers who don't speak MATLAB saw a lot of good grammar changes and approved it. Whilst I agree with you on the ambiguity, technically a `1 x n` cell is a row vector as well. Do take care to not interpret the OPs meaning when you edit, and refrain from editing code other than to properly indent it at all times – Adriaan May 23 '16 at 20:29

1 Answers1

3

You'll want to not hand-copy each row into a separate variable before doing this. The proper way using your desired for loop would be thus

A = rand(15,39);
vectors = cell(1,size(A,1)); % initialise output

for ii = 1:size(A,1) % loop over all rows
    vectors{1,ii} = A(ii,:); % store each row in the cell
end

To do this without a loop (thanks to @beaker)

B = mat2cell(A, ones(1,size(A,1)), size(A,2)).';

though a matrix (so your original A) would be the best overall, since MATLAB works best with matrices.

Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75