Column to row vector

214 ビュー (過去 30 日間)
Malachi Boodoo-Joseph
Malachi Boodoo-Joseph 2020 年 11 月 8 日
コメント済み: DGM 2025 年 8 月 31 日 10:59
How to extract and convert a column to a row vector so say I want to extract 5;10;9;25 how do I convert that to a row when I extract it?

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 8 日
You can use transpose()
x = [5;10;9;25];
y = x.'; % or y = transpose(x)
  2 件のコメント
Babak
Babak 2025 年 8 月 20 日 16:34
Isn't there a function that could do this? Basically keep a row as a row and convert a column vector into a row. Ensuring if x was a row it wouldn't take the `transpose`
Stephen23
Stephen23 2025 年 8 月 20 日 18:30
編集済み: Stephen23 2025 年 8 月 31 日 4:55
@Babak here are two approaches that work regardless of the input size:
x = [5;10;9;25]
x = 4×1
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = reshape(x,1,[]) % more efficient
y = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = x(:).' % less writing
y = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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


Babak
Babak 2025 年 8 月 20 日 16:38
編集済み: DGM 2025 年 8 月 31 日 10:28
x = [5;10;9;25]; % a column vector
reshape(x, 1, length(x)) % a row vector
ans = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = [5 10 9 25]; % a row vector
reshape(y, 1, length(y)) % still a row vector
ans = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% create an anonymous function to do the job
f = @(x)reshape(x,1, length(x));
% when applied to the previous vectors, the results are as before
f(x)
ans = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f(y) % and another
ans = 1×4
5 10 9 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
DGM
DGM 2025 年 8 月 31 日 10:59
Edited to run the code for demonstration purposes; added comments.
Note that there are few cases where length() can be used incautiously without inviting problems. Unless you're making sure that the input is strictly a vector, you're waiting on a confusing error about the wrong number of elements.
If you want to explicitly specify the output size, use numel() instead of length(). The usefulness of the output may be zilch for a reshaped matrix, but at least it avoids the confusing error and returns a vector. Specifying a slack dimension as in @Stephen23's example does the same, and is more succinct.
x = [1 2 3 4].'; % a column vector or row vector
y = reshape(x,1,[]) % to a row vector
y = 1×4
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = [1 3 5; 2 4 6]; % oops we didn't constrain the input dimensionality!
y = reshape(x,1,numel(x)) % but at least we got a vector without error
y = 1×6
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = reshape(x,1,[]) % same thing, more succinct, easier to read/remember
y = 1×6
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = reshape(x,1,length(x)) % you're trying to fit 6 elements in a 1x3 vector
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by