Divide an array in n different arrays

1 visualización (últimos 30 días)
luca
luca el 17 de Sept. de 2019
Editada: Bruno Luong el 17 de Sept. de 2019
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?

Respuesta aceptada

Bruno Luong
Bruno Luong el 17 de Sept. de 2019
Editada: Bruno Luong el 17 de Sept. de 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{:}

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by