Grouping the Elements of the Array

21 visualizaciones (últimos 30 días)
tinkyminky93
tinkyminky93 el 21 de Jun. de 2022
Comentada: Voss el 21 de Jun. de 2022
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 comentarios
Image Analyst
Image Analyst el 21 de Jun. de 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
tinkyminky93 el 21 de Jun. de 2022
Editada: tinkyminky93 el 21 de Jun. de 2022
I have a .mat file and i am importing them. I can see that it is double array and it is 897880x1. I just want to group them like above.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 21 de Jun. de 2022
% Taking your example of an array with 17 elements (rather than 897880),
data = 1:17;
% and you want to group them into groups of 5 (rather than 30000).
group_size = 5;
% You can append NaNs to the end of the data as necessary, to make the
% number of elements a multiple of 5:
% number of elements in data
n_data = numel(data);
% number of groups of 5 necessary
n_groups = ceil(n_data/group_size);
% append NaNs off the end of data, storing the result as new_data
new_data = data;
new_data(end+1:n_groups*group_size) = NaN;
% And finally reshape the NaN-appended data set:
new_data = reshape(new_data,group_size,[]).'
new_data = 4×5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 NaN NaN NaN
  4 comentarios
tinkyminky93
tinkyminky93 el 21 de Jun. de 2022
Yes, I know. Is it necessary to write NaN to those elements? What if I don't write anything?
Voss
Voss el 21 de Jun. de 2022
It is necessary to have something there. It doesn't have to be a NaN, but it's a good idea to use something so that you can distinguish that part from the rest of the data, i.e., discern where the data stops and the filled-in section begins.
Something must be there because a matrix must be rectangular in shape. It can't be some weird polygon shape with a corner missing. In other words, every row must have the same number of elements (or, put another way, every column must have the same number of elements). This is the defining characteristic of a matrix.
If you want something where the number of elements can vary by row, use a cell array.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 21 de Jun. de 2022
x = rand(897880, 1);
nx = numel(x);
len = 300000;
tile = [repelem(len, floor(nx / len)), rem(nx, len)]
tile = 1×3
300000 300000 297880
xC = mat2cell(x, tile, 1);
  2 comentarios
tinkyminky93
tinkyminky93 el 21 de Jun. de 2022
Suppose that I have 7 groups which have 30000 elements inside of it. And I want like 7x30000 array not a cell sir. From your code, I got 3x1 cell and every element of the cell has 30000 elements in it.
Jan
Jan el 21 de Jun. de 2022
Editada: Jan el 21 de Jun. de 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)
ans = 1×2
2 300000
size(Remainder)
ans = 1×2
1 297881

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by