Can I divide a linear array by a number which larger than its size ?

I am having a cell array. Each element of the cell array is a linear array . These arrays are of different sizes. when diving each array to some equal parts then I am not getting consistent parts. Suppose I want to divide each cell array by 20. When the size is greater than 20 then its ok. but when less than 20 then I am not getting any consistent parts. I want to divide each linear array to 20 equal parts. whatever the size is I dont want any padding of values. If the array size is 1×17 and diving with 20 will give 20 equal parts . The partition size will be less than 1. How can I do this ?

2 commentaires

I am getting "cell array.png' cell array. Now I want to divide each cell equally to 20 partitions
Unless you want to do some interpolation/resizing, you can't because each array in each cell is not a multiple of 20.

Connectez-vous pour commenter.

Réponses (2)

KSSV
KSSV le 16 Mar 2019
You interpolate your data into your desired size and then reshape. Read about interp1.

1 commentaire

how can I handle an entire cell array with this ? Suppose my first cell contains an array of size 1×17, diving with 20 will give a size of 0.85. Is this possible to do here ? Next cell contains 1×31 , diving with 20 again, next cell is 1×131 again diving with 20. Cell array contains 16 colums and 1000 rows . How can I loop over this process? Please help.

Connectez-vous pour commenter.

Next20 = @(V) ceil(length(V)/20) * 20;
Interp20 = @(V) interp1(V, linspace(1, length(V), Next20(V)));
Split20 = @(V) mat2cell(V, 1, 20*ones(1, length(V)/20));
new_cell = cellfun( @(V) Split20(Interp20(V)), cell_array, 'Uniform', 0);

8 commentaires

Walter Roberson: If I do resize or interpolation then I am not able to count number of occurrence of 1's from each partitions. Each cell array is linear array of consequtives 0's and 1's. After the equal partitions I want to count number of occrrence of 1's from each partitions.
Your task is not possible then. It is not possible to have a number composed of only 85% of the bits of a regular number so that you can divide 17 numbers into 20 slots without padding.
If what you want is to count the number of 1's, then why not pad with 0's, since that will not affect the total?
A{k,j}=profile;
ndivisions=32;
N{k,j} = length(A{k,j});
partnum = floor(1+(0:N{k,j}-1)/N{k,j}*ndivisions);
n1{k,j} = accumarray(partnum(:),A{k,j}(:)==1);
n2{k,j}=n1{k,j}.';
n= accumarray(partnum(:),1);
o=n.';
f{k,j}=n2{k,j}./o;
Walter Roberson: I am doing this and not getting any uniform partition size.
Of course you are not getting uniform partition size: you are not doing padding, so if the input is not an exact multiple of ndivisions, you cannot get uniform partition sizes.
A{k,j}=profile;
ndivisions=32;
N{k,j} = length(A{k,j});
N{k,j} = 32 * ceil(N{k,j}/ndivisions);
How to do padding in this m X n cell array ?
Error using zeros
Size inputs must be scalar.
Error in @(v,N)FirstN([v(:).',zeros(1,N)],N).'
Error in @(v)PadN(v,len)
Error in partition (line 67)
A = cellfun(@(v) PadN(v, len), A, 'uniform', 0);
I am always getting this error
Attach your m-file with the paper clip icon so we can solve this.

Connectez-vous pour commenter.

Question posée :

le 16 Mar 2019

Commenté :

le 17 Mar 2019

Community Treasure Hunt

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

Start Hunting!

Translated by