Assign column vector to a matrix row

This always works, but why? Is it legal?
vert = [1:4]',
horiz = [11:14],
mat = zeros(4,4)
mat(2,:)=vert,
mat(3,:)=horiz
My vector is a column vector return by a function. I'm hoping to avoid an extra step just to transpose it.

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 18 日
What is your problem?
Andrew Reibold
Andrew Reibold 2014 年 7 月 18 日
This question made me laugh really hard, but I'm going to try to maintain professionality.
What are you trying to ask when you say "Is it legal?" I'm assuming you don't mean "Is it against the law?" !

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2014 年 7 月 19 日
編集済み: Matt J 2014 年 7 月 19 日

0 投票

Yes, it is legal. From the documentation on SUBSASGN:
" For multidimensional arrays, a(I,J,K,...) = b assigns b to the specified elements of a. b must be length(I)-by-length(J)-by-length(K)-... or be shiftable to that size by adding or removing singleton dimensions. "
Here's another example where the mis-shaped right hand side of the assignment isn't even a vector,
>> mat=zeros(4); a=rand(1,1,2,3);
>> mat(1:2,1:3)=a
mat =
0.1779 0.8754 0.5206 0
0.5702 0.1890 0.4129 0
0 0 0 0
0 0 0 0

1 件のコメント

John Jeppson
John Jeppson 2014 年 7 月 19 日
Thanks. Excellent answer!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

質問済み:

2014 年 7 月 18 日

コメント済み:

2014 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by