MATLAB: Could anyone help me to solve the issue

iterations

I want to run the command line iwant2=max(iwant1,[],3) with respect to iterations. I tried with the following line iwant3(it)=iwant2.But i unable to get the result.Could anyone please help me on this.

Best Answer

a=[2 4 6 8 10 12]
c=1:a(t)
When t is 1, that is c=1:a(1) which is c=1:2
for s=1:numel(c)
so in the first iteration, s reaches as high as 2, and
iwant1(t,r,s) = sum(cell2mat(o_th(t,r,s,:)));
makes iwant1 into a 1 x 1 x 2 array.
iwant2=max(iwant1,[],3);
With iwant1 being 1 x 1 x 2, max() of that over the third dimension is going to give a 1 x 1 result for iwant2, and a scalar can be stored into a scalar location in
iwant12(t,r,s,it)=iwant2
Then consider when t becomes 2. The first iteration with that,
iwant1(t,r,s) = sum(cell2mat(o_th(t,r,s,:)));
is going to enlarge iwant1 from 1 x 1 x 2 into to 2 x 1 x 2 . max() of a 2 x 1 x 2 over the third dimension is going to return a 2 x 1 x 1 array, more commonly called a 2 x 1 array. And you cannot store a 2 x 1 array into a scalar location.
This is the same error we have been telling you about since... what, October or so, if not earlier?
If your different iterations are going to produce different sizes of output, then you need to use cell arrays, or you need to initialize your arrays to maximum size, initializing with a value that cannot be created otherwise, to signal locations that are "unused".