# how to insert a value into the array in a consistent manner.

11 views (last 30 days)
Yuki Koyama on 20 Nov 2019
Commented: Bhaskar R on 20 Nov 2019
If I want to get [0;1;1;1] when A=ones(3,1)=[1;1;1], I can get [0;1;1;1] by typing [0;A].
If I want to get [0 1 1 1] when A=ones(1,3)=[1 1 1], I can get [0 1 1 1] by typing [0 A].
How can I write them in the consistent way? Must I always use semicolon or space(or comma) dependign on whether A is a row vector or a column vector?
Stephen23 on 20 Nov 2019
Edited: Stephen23 on 20 Nov 2019
"Must I always use semicolon or space(or comma) dependign on whether A is a row vector or a column vector? "
If you want to use the concatenation operator [] then a comma/space or semi-colon is required.
"How can I write them in the consistent way?"
What does "consistent" mean? In one case you are adding a new row, in the other case a new column: do you expect to be able to use the same syntax for both of these?

Stephen23 on 20 Nov 2019
Edited: Stephen23 on 20 Nov 2019
>> A = ones(3,1);
>> A(end+1) = 0;
>> A([2:end,1]) = A
A =
0
1
1
1

Bhaskar R on 20 Nov 2019
Are you asking for this ?
A = ones(3, 1);
if (size(A,2)>1)
A = [0 A];
else
A = [0;A];
end
##### 2 CommentsShowHide 1 older comment
Bhaskar R on 20 Nov 2019
Yeah, I have been learning/growing with new commands, ways and techniques as a new contributer in this forum. Thanks for the feedback of the answer. Its my pleasure.!!