Spliting a n-by-3 Array into several different arrays.

So, I have the following array:
v =
[1 2 3;
-1 2 -3;
0 0 0;
1 -2 3;
1 2 3;
1 2 3;
0 0 0;
0 0 0;
1 1 1]
I would like to split it into several arrays that I can call upon later as follows:
v_1 = [1 2 3; -1 2 -3],
v_2 = [1 -2 3; 1 2 3; 1 2 3],
v_3 = [1 1 1];
How would I go about doing this. I would like to use the zeros as an indication of where to create another variable with the array indicated.
Thank you.

5 comentarios

Scott Salazar
Scott Salazar el 6 de Sept. de 2018
Editada: Scott Salazar el 6 de Sept. de 2018
I was hoping to use what Star Strider did in his example, and make a variation on it. Would this be a proper approach to my problem?
So, I have something now, looks like the following:
v = [1 2 3; -1 2 -3; 0 0 0; 1 -2 3; 1 2 3; 1 2 3; 0 0 0; 0 0 0; 1 1 1];
idx = all((v),2);
idr = diff(find([1;diff(idx);1]));
D = mat2cell(v,idr(:),size(v,2));
D{1:2:end}
Where the value of D{1:2:end} is:
ans =
1 2 3
-1 2 -3
ans =
1 -2 3
1 2 3
1 2 3
ans =
1 1 1
Which is close to what I want, but I would again like to call them each from a vector. Thoughts?
Excuse me, I meant to ask that the output will be in several different arrays.
dpb
dpb el 7 de Sept. de 2018
I know you asked that, but do NOT go down that perilous pathway..."There be dragons!"
Stephen23
Stephen23 el 7 de Sept. de 2018
"...but I would again like to call them each from a vector. Thoughts?"
Do NOT do that. Magically accessing variable names is one way the beginners force themselves into writing slow, complex, buggy code. Here is why:

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 6 de Sept. de 2018
v is not a vector but an array.
Star's code works on a vector; would be painful to extend to an array I think, as is. BUT, you can create a vector to use to find the split locations by his technique and then select the array instead of vector.
Define the auxiliary vector as
aux=all(v==0,2);

Más respuestas (0)

Categorías

Más información sobre Elementary Math en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Sept. de 2018

Comentada:

el 7 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by