How is this even logical?

1 view (last 30 days)
Andreas Karlsson
Andreas Karlsson on 10 May 2023
Commented: Stephen23 on 11 May 2023
As you can see, y1 is a column vector.
When inserted at position 1 in y, it becomes a row vector.
Why and how do I not make it do this?
  1 Comment
Stephen23
Stephen23 on 11 May 2023
"When inserted at position 1 in y, it becomes a row vector."
Your description is incorrect: the indexing y(1,:) is not "position 1", it is the entire first row of y. Not the same thing at all.
In any case, you did not explain what you expect to occur: what should MATLAB do when you try to force three rows of data into one row?

Sign in to comment.

Answers (2)

James Tursa
James Tursa on 10 May 2023
Edited: James Tursa on 11 May 2023
When assigning into rows or columns via indexing, MATLAB will conform a RHS vector from row or column to row or column if necessary for the assignment.
E.g.,
x = 1:3 % a row vector
x = 1×3
1 2 3
z = zeros(3)
z = 3×3
0 0 0 0 0 0 0 0 0
z(1,:) = x % assign elements of x into 1st row, works as expected
z = 3×3
1 2 3 0 0 0 0 0 0
z(:,1) = x % this works too because of the auto conforming behavior, into a column
z = 3×3
1 2 3 2 0 0 3 0 0
z(1:4:end) = x % and assigning into linear indexing also works into the diagonal
z = 3×3
1 2 3 2 2 0 3 0 3
If you don't want this behavior, please provide an example of all inputs and desired output and we can probably help you with the syntax.

Walter Roberson
Walter Roberson on 10 May 2023
When inserted at position 1 in y
However, assigning to y(1,:) is not asking to insert at "position 1" -- the vector is length 3 and "position 1" would be a scalar, and you cannot store a vector with three elements into a location only big enough for a scalar.
Assigning to y(1,:) is asking to assign to row 1.
With the value to be assigned being a column vector, there are only two reasonable things that MATLAB could do:
  1. Give an error message because the shape of the destination is not exactly the shape of the source; or
  2. Go ahead and silently reshape the source column to fit into the destination row.
Giving an error message would certainly be a possibility. But is it useful ?
Suppose I gave you a task: Initialize A to be a 4 x 1 array of zeros, and copy the first three elements of the input vector y into the first three elements of A.
With the way MATLAB works at the moment, that becomes very easy:
A = zeros(4,1); A(1:3) = y(1:3);
But if MATLAB gave error messages for assigning between rows and columns (of the same number of elements) then it would have to look more like
A = zeros(4,1);
if size(y,1) == 1;
A(1:3) = y(1:3).';
else
A(1:3) = y(1:3);
end
or
A = zeros(4,1); A(1:3) = reshape(y(1:3),[],1);
Now suppose the task is to accept an input vector A and copy the first three elements of y into the first three elements of A.
if size(A,1) == 1;
A(1,1:3) = reshape(y(1:3), 1, []);
else
A(1:3,1) = reshape(y(1:3), [], 1);
end
Yes, it is do-able, but some kind of "type purity" of "row vector is a different type than a column vector" truly important compared to the convenience of being able to just do A(1:3) = y(1:3); ?

Community Treasure Hunt

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

Start Hunting!