matlab - Convert rows of a matrix to vectors -
i have matrix lets say:
a=[1 2 1; 5 6 7; 7 8 9]
and want extract rows in following format:
x_1=[1 2 1] x_2=[5 6 7] x_3=[7 8 9]
i want know how can write x_1
, x_2
, x_3
. know how extract rows don't know how make x_1
, x_2
, x_3
. i want automatic, because real matrix has large size , don't want make x_1 x_2 .. x_100
hand.
you can try following:
m = size(a,1); i=1:m % set variable name varname = sprintf('x_%d',i); % create , assign variable in base workspace assignin('base',varname,a(i,:)); end
the code iterates through every row of a, creates variable name (as per format) , assigns variable in matlab base workspace ('base') data being ith row of a.
if doing function, rather using 'base' use 'caller' indicate variables should created in workspace of function.
Comments
Post a Comment