# Problem with accumarray and constructing an array with accumulation

2 views (last 30 days)
AA on 19 May 2020
Answered: Steven Lord on 19 May 2020
for k=1:24
n =24;
for jj = numel(n):-1:1
cd = circshift(out2hourly{1}, [-(k-1) 0]);
n1 = ceil((1:size(cd,1))'/n(jj));
out{k} = [cd(1:n(jj):end,1), accumarray(n1,cd(:,2),[],@max),...
accumarray(n1,cd(:,3),[],@min),cd(24:n(jj):end,4)];
end
end
Suppose the formula above. Out2hourly is a cell array that contains a matrix with 4 columns and 4000-5000 rows. I want to construct an array with accumulation meaning every 24 rows are summarized to one. The first column has the first value, the second column the maximum value of the 24 rows, the third column has the minimum value of the 24 rows and the last column contains the very last value of the 24 chain. The problem is that the last column will be x-1 rows. The rows of the columns 1-3 have x rows. Therefore I get the following error message:
Error using horzcat
Dimensions of arrays being concatenated are not consistent
I can solve this issue by removing the last row of the first three columns. Therefore I have rewritten the file as below but somehow it doesnt work and I get another error message
out{k} = [cd(1:n(jj):end-1,1), accumarray(n1,cd(1:end-1,2),[],@max),...
accumarray(n1,cd(1:end-1,3),[],@min),cd(24:n(jj):end,4)];
Does anyone haven an idea how to resolve this?

Steven Lord on 19 May 2020
In your accumarray calls you're specifying the third input (sz) as an empty array, which tells accumarray basically "you figure out how large the result should be." If you know how large you want it to be, provide that information to accumarray.
locations = [1 2; 2 1];
values = [2; 3];
A = accumarray(locations, values, [])
B = accumarray(locations, values, [3 3])
In the example above, A is 2-by-2. It doesn't need any more rows and columns to contain the values at the coordinates given in the locations matrix. B is 3-by-3, as while the values do technically fit in a 2-by-2 matrix you explicitly asked for the result to be 3-by-3.