Clear Filters
Clear Filters

Solve looping for big cell, vector, and summation

2 views (last 30 days)
DoinK
DoinK on 14 Jun 2023
Edited: Matt J on 14 Jun 2023
I have Z, Z is cell (1*1000) and for i=1:1000 size(Z{i}) is 1*1008
I need to do the summation until 1000, start from 1, i can do it manually but it will be long until 1000.
The code is like this,
p1=sum(reshape(Z{1},[],4),1) %Z{1} is 1*1008, from 1008 column reshape to 4 column
%4*column1
%5*column2
%6*column3
%7*column4
for i=1:4
f=(i+3)*p1(i) % x*p(x)
h(i)= f %size(1*4)
end
sum(h)
Repeat,
p2=sum(reshape(Z{2},[],4),1)
%8*column1
%9*column2
%10*column3
%11*column4
for j=1:4
f=(j+7)*p1(j) % x*p(x)
h(j)= f %size(1*4)
end
sum(h)
How to create a looping until p1000?

Answers (1)

Matt J
Matt J on 14 Jun 2023
Edited: Matt J on 14 Jun 2023
Z=repmat({rand(1,1008)}, 1,1000); whos Z %Fake input data
Name Size Bytes Class Attributes Z 1x1000 8168000 cell
Z=reshape(cat(3,Z{:}) ,[],4,numel(Z));
p=sum(Z,1);
w=reshape(0:numel(p)-1,1,4,[]);
result = squeeze( sum( p.*w,2) ); whos result
Name Size Bytes Class Attributes result 1000x1 8000 double
  2 Comments
DoinK
DoinK on 14 Jun 2023
The last result is like this Sir,
w(:,:,63000) =
251996 251997 251998 251999
And pop out error message :
"Arrays have incompatible sizes for this operation.
Related documentation "

Sign in to comment.

Categories

Find more on Historical Contests 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!