Effacer les filtres
Effacer les filtres

How to put array in a cell of different groups ?

2 vues (au cours des 30 derniers jours)
Heirleking
Heirleking le 18 Avr 2022
I have this array, A, and need it to look like the cell B
A = [ 70; 63; 105; 95; 128; 123; 137; 132; 142; 143; 154; 154; 160; 143 ];
B = { [ 70; 63 ] [ 105; 95 ] [ 128; 123 ] [ 137; 132 ] [ 142; 143 ] [ 154; 154 ] [ 160; 143 ]}
I was using histcounts and puting them in bins but could not make it work for the last cell
[~,~,bins]=histcounts(A,[60 70 80 90 100 110 120 130 140 150 160 170]);
u=unique(bins);
for k=1:numel(u)
groups{k}=A(bins==u(k));
end

Réponse acceptée

Voss
Voss le 18 Avr 2022
A = [ 70; 63; 105; 95; 128; 123; 137; 132; 142; 143; 154; 154; 160; 143 ];
B_wanted = { [ 70; 63 ] [ 105; 95 ] [ 128; 123 ] [ 137; 132 ] [ 142; 143 ] [ 154; 154 ] [ 160; 143 ]};
B = num2cell(reshape(A,2,[]),1);
isequal(B,B_wanted) % the result B matches the desired result B_wanted
ans = logical
1

Plus de réponses (1)

David Hill
David Hill le 18 Avr 2022
A = [ 70; 63; 105; 95; 128; 123; 137; 132; 142; 143; 154; 154; 160; 143 ];
for k=1:length(A)/2
B{k}=A(2*(k-1)+1:2*k);
end

Catégories

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

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by