フィルターのクリア

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 Center および File ExchangeResizing 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