i f i have an array a=[1 2 3 4 1;6 7 8 9 2].I need to split this array based on the value in last column.Resultant array b=[1 2 3 4],c=[6 7 8 9]. The splitted array b should contain the row whose last column is 1 and c 2

1 view (last 30 days)
a=[1 2 3 4 1 5 6 7 8 2 3 4 5 6 1 4 3 2 1 1] Resultant matrix is : b=[1 2 3 4 3 4 5 6 4 3 2 1] c=[5 6 7 8]

Answers (1)

Julia
Julia on 28 Oct 2014
Edited: Julia on 28 Oct 2014
Hi,
here is a solution for your small problem. If a is larger, you can use a loop.
b=[];
c=[];
if a(1,end)==1
b=[b,a(1,1:end-1)];
end
if a(2,end)==2
c=[c,a(2,1:end-1)];
end
Or with switch-case. Something like:
switch a(i,end) % where i is a counting index of a loop
case 1
b=[b,a(i,1:end-1)];
case 2
c=[c,a(i,1:end-1)];
end

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!