how can i save row value in variable like x(1)

i have metric row =[1 1 0 0; 1 1 0 0; 0 1 1 0 ; 1 1 0 0] how i can make x(1)=1 1 0 0 and x(2)= 1 1 0 0 and so on

 Accepted Answer

>> row =[1 1 0 0; 1 1 0 0; 0 1 1 0 ; 1 1 0 0] ;
x = row;
x(1,:)
ans =
1 1 0 0
>> x(2,:)
ans =
1 1 0 0
>>

1 Comment

can i make for loop and write
for i=1:4
x(i)=.......
i mean x(1)=1 1 0 0

Sign in to comment.

More Answers (1)

What you ask is not possible in MATLAB. You two options (using a for loop) are:
for i = 1 : 4
x(i,:) = row(i,:)
end
or
for i = 1 : 4
x{i} = row(i,:);
end
In the first of those options, you would need to access x(1,:) to get the first row as a numeric vector.
In the second of those options, you would need to access x{1} to get the first row as a numeric vector. Notice that is not x(1), it is x{1}. If you were to use x(1) then what you would get would be {[1 1 0 0]} which is a cell array containing the vector rather than the vector itself.

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!