MATLAB Answers

How to split an array?

2 views (last 30 days)
aa on 11 Sep 2020
Commented: Star Strider on 12 Sep 2020
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,
If we have 7 as a spliting number than the output is
I need generazlied function to compute this.


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 11 Sep 2020
Edited: Star Strider on 11 Sep 2020
Use logical indexing‘:
b1 = a(a<7)
b2 = a(a>7)
b1 =
1 4 6
b2 =
9 13 17 20
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.


Image Analyst
Image Analyst on 11 Sep 2020
And might also want <= or >= in case 7 exactly is in there.
aa on 12 Sep 2020
hi , see this .. where to inset loops for generalzed calculation for each column
Star Strider
Star Strider on 12 Sep 2020
This could work:
if (all(a > c)) || (all(a < c))
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.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!