Divide an array in n different arrays

1 view (last 30 days)
Hi, given the array
V = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 1 1 1 1 1 6 6 6 6 6 6 6]
and the matrix
A = [1 3 4
6 5 2
0 8 0
0 9 0]
I want to obtain 5 different arrays
a = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6];
b = [ 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9];
c = [4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 ];
d = [1 1 1 1 1 6 6 6 6 6 6 6];
so every time I meet the elements of one of the columns: 1-6 , 3-5-8-9 or 4-2 , I have to collect them and reported in a new array.
N.B zeros are never present in V, but are just inserted in A to create a square matrix
May someone help me with this task?

Accepted Answer

Bruno Luong
Bruno Luong on 17 Sep 2019
Edited: Bruno Luong on 17 Sep 2019
V = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 1 1 1 1 1 6 6 6 6 6 6 6]
A = [1 3 4
6 5 2
0 8 0
0 9 0]
[~,c,a] = find(A);
[~,i] = ismember(V,a);
g = c(i);
d = find([true; diff(g); true]);
c = mat2cell(V,1,diff(d));
c{:}

More Answers (0)

Categories

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

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!