Split vector into 2 variables

9 ビュー (過去 30 日間)
Jorge Bastillo
Jorge Bastillo 2015 年 1 月 28 日
編集済み: Stephen23 2015 年 2 月 7 日
I specifically want to use length and fix to split a vector (of an even or odd number) into two halves and then assign each half a variable.

採用された回答

Stephen23
Stephen23 2015 年 1 月 28 日
編集済み: Stephen23 2015 年 1 月 28 日
Something like this?:
>> A = [101,102,103,104,105];
>> X = 1:numel(A) < 4;
>> B = A(X)
B =
101 102 103
>> C = A(~X)
C =
104 105
  2 件のコメント
Jorge Bastillo
Jorge Bastillo 2015 年 1 月 28 日
I should have been more specific, my apologies.
Your code does work, however I want the user to enter the vector and my code to use the length and fix functions to accomplish this. You would have the right output for this one, however it would only work on this vector with 5 elements. It wouldn't work for a vector with 3 elements.
Stephen23
Stephen23 2015 年 1 月 31 日
編集済み: Stephen23 2015 年 2 月 7 日
You can adjust the compared value to anything you would like to, including half the vector length. This will work for vectors of any length:
>> A = [101,102,103,104,105];
>> X = 1:numel(A) < numel(A)/2;
>> B = A(X)
B =
101 102
>> C = A(~X)
C =
103 104 105
This will automatically adjust to any length of vector A. Note that if the vector A has an odd number of elements, then C will have one more element than than B.

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

その他の回答 (0 件)

カテゴリ

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