Borrar filtros
Borrar filtros

How to put array in a cell of different groups ?

2 visualizaciones (últimos 30 días)
Heirleking
Heirleking el 18 de Abr. de 2022
Respondida: David Hill el 18 de Abr. de 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

Respuesta aceptada

Voss
Voss el 18 de Abr. de 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

Más respuestas (1)

David Hill
David Hill el 18 de Abr. de 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

Categorías

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

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by