How can simply divide a linear array into 32 equal parts?

조회 수: 24 (최근 30일)
Zara Khan
Zara Khan 2019년 3월 6일
댓글: Walter Roberson 2019년 3월 7일
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
Walter Roberson
Walter Roberson 2019년 3월 6일
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
Zara Khan 2019년 3월 6일
I am always getting 1D arrays of different lengths like 1×31, 1×62, 1×93, 1×124,1×155,1×185,1×216,1+247

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2019년 3월 6일
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
Walter Roberson
Walter Roberson 2019년 3월 7일
they are dummy parameter for anonymous functions that make the code easier
Walter Roberson
Walter Roberson 2019년 3월 7일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by