currently i am working with a matrix. The dimension of the matrix is 100*2500. I need to assign each row the matrix to a new variable. How i can do this?


closed as off-topic by MBaz, Peter K. Nov 1 '17 at 1:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "General programming questions are off-topic here, but can be asked on Stack Overflow." – MBaz, Peter K.
If this question can be reworded to fit the rules in the help center, please edit the question.


You can assign each row to a vector as follows:

row1 = matrix(1,:);

That will store the whole first row of matrix in row1.

To do this "automatically", you can store all the vectors containing each row in a cell array, for example:

for k = 1:size(matrix,1) %For each row
    rows{k} = matrix(k,:); %Store the k-th row in the k-th element of the cell array
  • $\begingroup$ this will give a cell array. But i need a row matrix. thank for your help. $\endgroup$ – MM Islam Nov 1 '17 at 1:20
  • $\begingroup$ What do you mean by row matrix? I'm not sure I understand what you are looking for. $\endgroup$ – Tendero Nov 1 '17 at 1:22
  • $\begingroup$ sorry. what i meant was row vector. $\endgroup$ – MM Islam Nov 1 '17 at 1:31
  • $\begingroup$ let me clear it. let data = ones(3,5); so i want row1= data(1,:), row2=data(2,:) and row3= data(3,:). $\endgroup$ – MM Islam Nov 1 '17 at 1:32
  • $\begingroup$ @MMIslam try this code for i=1:size(matrix,1); eval(sprintf('row%d = matrix(i,:);', i)); end; However, first, this method is very very computationally inefficient. And second, this kind of question is off-topic. $\endgroup$ – AlexTP Nov 1 '17 at 8:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.