Grouping the Elements of the Array
古いコメントを表示
Hello, I have a double array with the element number of 897880. I want to group them like 30000 30000 but it gives me error because of the 897880 divided by 30000 is not an integer. How can I group it like 30000 30000 and the last grouped part as it is, if it is not the multiple of 30000? Thank you.
4 件のコメント
Radu Andrei Matei
2022 年 6 月 21 日
Hi, I can't understand what you want to achieve, especially the "and the last element as it is" part. Could you try to rephrase your question?
tinkyminky93
2022 年 6 月 21 日
編集済み: tinkyminky93
2022 年 6 月 21 日
Image Analyst
2022 年 6 月 21 日
What form are these numbers in? They can't be in a double array because matrices can't have ragged right edges. Maybe you have a cell array. Or do you have one long vector of 897880 elements and you want to make it square with the last few elements padded with zeros?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
tinkyminky93
2022 年 6 月 21 日
編集済み: tinkyminky93
2022 年 6 月 21 日
採用された回答
その他の回答 (1 件)
x = rand(897880, 1);
nx = numel(x);
len = 300000;
tile = [repelem(len, floor(nx / len)), rem(nx, len)]
xC = mat2cell(x, tile, 1);
2 件のコメント
tinkyminky93
2022 年 6 月 21 日
In your question you have 897880 elements and want 3 groups. Because the last group is smaller than the others, you cannot simply use reshape. Therefore I create 3 different arrays and store them in a cell.
Of course you can store the first groups with the matching sizes in a matrix also:
x = rand(897880, 1);
len = 300000;
nx = numel(x);
match = nx - mod(nx, len);
Groups = reshape(x(1:match), len, []).';
Remainder = x(match:nx).';
size(Groups)
size(Remainder)
カテゴリ
ヘルプ センター および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
