필터 지우기
필터 지우기

How to put array in a cell of different groups ?

조회 수: 2 (최근 30일)
Heirleking
Heirleking 2022년 4월 18일
답변: David Hill 2022년 4월 18일
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

채택된 답변

Voss
Voss 2022년 4월 18일
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

추가 답변 (1개)

David Hill
David Hill 2022년 4월 18일
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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by