How can I divide a linear array into 8 equal parts?

A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
This is a long linear array with consecutive 0's and 1's. How can I equally divide this array A into 8 equal parts and count no of 1's from each parts?

댓글 수: 5

Hi,
B = reshape(A,5,[])
There are 125 elements in A, hence it cant be equally divide by 8.
How to count no of 1's from each parts ? And what will be the code when the array size is not predefined?
How to count no of 1's from each parts ?
if let say we divide into 5 rows:
A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
B = reshape(A,[],5);
for i=1:1:5
partSum(i) = sum(B(:,i))
end
Does it work for you?
what will be the code when the array size is not predefined?
numEle = numel(A);
for i=1:1:10
try
B = reshape(A,i,[]);
catch
fprintf('array size is not correct %i \n',i);
end
end
Then it will tell you what is the array size you can reshape them.
partSum(i) = sum(B(:,i))
this is always giving same values.
partSum =
0 5 10 0 0
partSum =
0 5 10 0 0
partSum =
0 5 10 0 0
partSum =
0 5 10 0 0
partSum =
0 5 10 0 0
I want to count only no of 1's from each of the 5 parts.
Kevin Chng
Kevin Chng 2018년 10월 8일
편집: Kevin Chng 2018년 10월 8일
Just noticed the for-loop is not necessary.
B = reshape(A,[],5);
partSum = sum(B)
partSum =
0 5 10 0 0
0 is the total number of 1's in first part, 5 is the second part, 10........so on.
If want be more specific for better understanding, we could
first part = B(:,1);
second part = B(:,2);
third part = B(:,3);
fourth part = B(:,4);
fifth part = B(:,5);
then either use sum(first part) or nnz(firstpart) to count the number of 1's

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

답변 (2개)

KSSV
KSSV 2018년 10월 8일
You can get non zeros using nnz.
B = reshape(A,[],5)' ;
N = zeros(size(B,1),1) ;
for i = 1:size(B,1)
N(i) = nnz(B(i,:)) ;
end

댓글 수: 3

I want to keep each of the part into linear as well.
KSSV
KSSV 2018년 10월 8일
I want to keep each of the part into linear as well. ?????example please.
If I am diving a linear array into 8 equal parts then each 8 parts will be also linear. For example [0 0 1 1 0 1], [0 1 1 1 1 ] and so on.

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

Kevin Chng
Kevin Chng 2018년 10월 8일
편집: Kevin Chng 2018년 10월 9일
How to count no of 1's from each parts ?
if let say we divide into 5 rows:
B = reshape(A,[],5);
partSum = sum(B);
you could nnz or sum.
What will be the code when the array size is not predefined?
A=[1 2 3; 4 5 6; 7 8 9]
numEle = numel(A);
for i=1:1:10
numberofsize = rem(numEle,i);
if numberofsize ~= 0
fprintf('array size is not correct for %i \n',i)
end
end
Then it will tell you what is the array size you can reshape them.

댓글 수: 7

where you are using this numel ?
To check which size i am allowed to reshape them.
Ok. But in the loop I can't find this numEle.
Sorry for my mistake. Edited my answer.
NumEle is giving the length of the array. In reshape I think We need to send array name . Here it is A not numEle.
Kevin Chng
Kevin Chng 2018년 10월 9일
편집: Kevin Chng 2018년 10월 9일
Ya, you are right, it is my mistake.
NumEle is counting the number of element. Ya, you are right, i should use rem() instead. If the remainder is not 0, the size is not suitable for our reshape size.
Does it working for you?

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 10월 8일

댓글:

2018년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by