MATLAB Answers

Splitting matrix into row vectors

111 views (last 30 days)
Pedro Oliveira
Pedro Oliveira on 28 Nov 2017
Edited: Stephen Cobeldick on 28 Nov 2017
Hi,
I have a matrix (with varying dimensions so those aren't important) and I want to split it into vectors such that the first vector is the first row, the second vector is the second row, etc.
I need to do this since these vectors will be the input to a function that can only take row vectors. What is the best way to split the matrix into row vectors?
Thank you.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 28 Nov 2017
Edited: Stephen Cobeldick on 28 Nov 2017
Simply use indexing:
mat = [...]; % your matrix
for k = 1:size(mat,1)
vec = mat(k,:); % each row of mat
... do whatever with vec
end
C = num2cell(mat,2);
for k = 1:numel(C)
vec = C{k};
... do whatever with vec
end

  0 Comments

Sign in to comment.

Sign in to answer this question.