Grouping the Elements of the Array
Afficher commentaires plus anciens
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 commentaires
Radu Andrei Matei
le 21 Juin 2022
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
le 21 Juin 2022
Modifié(e) : tinkyminky93
le 21 Juin 2022
Image Analyst
le 21 Juin 2022
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
le 21 Juin 2022
Modifié(e) : tinkyminky93
le 21 Juin 2022
Réponse acceptée
Plus de réponses (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 commentaires
tinkyminky93
le 21 Juin 2022
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)
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
