How to put array in a cell of different groups ?

1 view (last 30 days)
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

Accepted Answer

Voss
Voss on 18 Apr 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

More Answers (1)

David Hill
David Hill on 18 Apr 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

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!