How to extract elements from an array in three different array

36 vues (au cours des 30 derniers jours)
sneha sharma
sneha sharma le 8 Déc 2015
Commenté : Guillaume le 10 Déc 2015
suppose i have an array which stores 19 elements. How to extract first seven elements in one, then next five in another and remaining in third.

Réponse acceptée

Guillaume
Guillaume le 8 Déc 2015
A = randi(100, 1, 19)
splits = [7 5];
B = mat2cell(A, 1, [splits, numel(A) - sum(splits)]);
celldisp(B)
Use cell arrays rather than numbered variables.
  2 commentaires
sneha sharma
sneha sharma le 9 Déc 2015
will you please elaborate how it is working
Guillaume
Guillaume le 10 Déc 2015
I'm not sure what you want me to elaborate on. You just pass the length of the submatrices you want the main matrix to be divided into, to mat2cell.

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 8 Déc 2015
One way:
A = rand(1,19);
B1 = A(1:7);
B2 = A(8:12);
B3 = A(13:end);

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by