How can simply divide a linear array into 32 equal parts?
Show older comments
I want to divide a linear array of consequtives 0's and 1's into 32 equal parts. Then I want to store number of occurrence of 1's of each parts and size of each parts. I am working with 1000 number of images. For each image I am getting 8 different length linear array.
5 Comments
KSSV
on 6 Mar 2019
What is size of your linear array? It should be multiple of 32..is it?
Zara Khan
on 6 Mar 2019
KSSV
on 6 Mar 2019
You can reshape...if it is exactly divisble by 32...else you have to either add or trim extra values.
Walter Roberson
on 6 Mar 2019
if the number of elements in the array is not a multiple of 32 then you cannot get 32 equal parts. You need to define what you want to do in this case.
You also need to define what you want if the number of elements is divisible by 32 but the rows and columns are not. For example 240 x 240 does not have rows or columns divisible by 32 but can be divided into 32 by taking either pairs of rows or pairs of columns .
Zara Khan
on 6 Mar 2019
Answers (1)
KSSV
on 6 Mar 2019
A = rand(1,247) ; % you data
a = length(A) ;
n = 32 ;
b = a + (n - rem(a,n)) ; % Get number divisible by 32
B = zeros(1,b) ;
B(1:a) = A ; % This pad extra zeros
iwant = reshape(B,b/n,[]) ;
11 Comments
Zara Khan
on 6 Mar 2019
KSSV
on 6 Mar 2019
If you have data in hand...it is like you know the size.
Zara Khan
on 6 Mar 2019
KSSV
on 6 Mar 2019
YOu can access cell array susing flower braces. {}
Zara Khan
on 6 Mar 2019
Zara Khan
on 6 Mar 2019
KSSV
on 6 Mar 2019
YOu should access cell array such that you get a row vector.....
Walter Roberson
on 6 Mar 2019
FirstN = @(v, N) v(1:N);
PadN = @(v, N) FirstN( [v(:).', zeros(1,N)], N) .'; %produces columns
projectdir = pwd;
dinfo = dir(fullfile(projectdir, '*.txt'));
filenames = fullfile(projectdir, {dinfo.name});
numfiles = length(filenames);
datacell = cell(numfiles,1);
for K = 1 : numfiles
thisdata = load(filenames{K});
datacell{K} = thisdata;
end
maxlen = max( cellfun(@length, datacell) );
maxlen = 32 * ceil(maxlen/32); %round up to next multiple of 32
datacell = cellfun(@(v) PadN(v, maxlen), datacell, 'uniform', 0);
splitcells = cellfun(@(v) mat2cell(v, 1, 32 * ones(1,maxlen/32)), datacell, 'uniform', 0);
Now splitcells will be a cell array with one element for each file. Each element will be a cell arrays of 32 vectors. The size of the vectors will be the same for all of the files.
Note that some of the vectors could end up being all zero, if one of the files was more than 33/32 times longer than another.
Zara Khan
on 7 Mar 2019
Walter Roberson
on 7 Mar 2019
they are dummy parameter for anonymous functions that make the code easier
Walter Roberson
on 7 Mar 2019
The anonymous function
FirstN = @(v, N) v(1:N);
is nearly equivalent to having created a function
function result = FirstN(v, N)
result = v(1:N);
end
That is, take an array as input in the first argument, and take a length in the second argument, and return the first that-many elements of the array.
You can pad an array to a consistent size, say N, by taking an array, appending N zeros to it (so now it is always at least N entries long), and then taking the first N values of the result.
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!