How to split an array?

2 ビュー (過去 30 日間)
aa
aa 2020 年 9 月 11 日
コメント済み: Star Strider 2020 年 9 月 12 日
Hi everyone,
May someone help me.
I have data in column and want to split this in two set by the condition of third point:
For example,
a=[1,4,6,9,13,17,20];
If we have 7 as a spliting number than the output is
[1,4,6]
and
[9,13,17,20]
I need generazlied function to compute this.

採用された回答

Star Strider
Star Strider 2020 年 9 月 11 日
編集済み: Star Strider 2020 年 9 月 11 日
Use logical indexing‘:
a=[1,4,6,9,13,17,20];
b1 = a(a<7)
b2 = a(a>7)
producing:
b1 =
1 4 6
b2 =
9 13 17 20
EDIT —
A ‘generalized function’ version would be something like this:
vsplit = @(v,n) {v(v<n); v(v>n)};
b = vsplit(a,7)
b1 = b{1}
b2 = b{2}
The result is the same.
  3 件のコメント
aa
aa 2020 年 9 月 12 日
hi , see this .. where to inset loops for generalzed calculation for each column
Star Strider
Star Strider 2020 年 9 月 12 日
This could work:
if (all(a > c)) || (all(a < c))
return
end
I tested that as well as I could. I runs without error and (if I understand correctly what you want to do) produces the correct result.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by