How is this even logical?

1 次查看(过去 30 天)
Andreas Karlsson
Andreas Karlsson 2023-5-10
评论: Stephen23 2023-5-11
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 个评论
Stephen23
Stephen23 2023-5-11
"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?

请先登录,再进行评论。

回答(2 个)

James Tursa
James Tursa 2023-5-10
编辑:James Tursa 2023-5-11
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 2023-5-10
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); ?

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by