How can I calculate fzero from values of matrices in cell array?

5 views (last 30 days)
Dear All, I have three cell arrays: cv_o, std_m and mean_m. Both cells contains 359 2D matrices. The size of matrices is same. I would like to calculate the root of an equation using the values of these matrices. I tried to calculate fzero by the following way:
for jj=1:359;
for i=1:464;
for j=1:201;
mat(i,j)=cv_o{jj};
mstd(i,j)=std_m{jj};
mmean(i,j)=mean_m{jj};
if mat(i,j) == 0;
b(i,j) = 0;
else
b(i,j)=fzero(@(x)mat(i,j)-(mstd(i,j)./mmean(i,j)).^x,(0));
end
end
end
end
But I got the following error message: Subscripted assignment dimension mismatch. Could someone write me how I should solve calculate the root?

Accepted Answer

Michael Haderlein
Michael Haderlein on 20 Apr 2015
Edited: Michael Haderlein on 20 Apr 2015
Dear Beáta,
Check this line (same holds for the next two lines):
mat(i,j)=cv_o{jj};
As you said, cv_o{jj} will return a matrix; but mat(i,j) is one scalar value and this mismatch is causing the error. Given that varname(i,j) returns a scalar, you'll see that also your equation can be solved analytically: x=log(mat(i,j))/log(mstd(i,j)/mmean(i,j)) I guess you want to have matrix operations here, but right now it's all scalar. Possibly you just need to remove all those (i,j), but I cannot say that for sure. Also, you'll not get b(i,j) but rather b(jj).
  2 Comments
Szabó-Takács Beáta
Szabó-Takács Beáta on 22 Apr 2015
Thank you! Meantime I tried the code you that suggested and it works.

Sign in to comment.

More Answers (1)

Szabó-Takács Beáta
Szabó-Takács Beáta on 21 Apr 2015
Dear Michael, Thank you for your help! I changed the calculation according to your suggestion by the following way:
for jj=1:360;
for i=1:464;
for j=1:201;
mat=cv_o{jj};
mstd=std_m{jj};
mmean=mean_m{jj};
if mat(i,j) == 0;
b(i,j)=0;
elseif mat(i,j) > 0;
b(i,j)=fzero(@(x)mat(i,j)-(mstd(i,j)./mmean(i,j)).^x,(0.1));
BB{jj}=b;
end
end
end
end
It calculates the power b until jj=29. After that I got the following message: Error using fzero (line 309) Function value at starting guess must be finite and real. I do not understand why? I checked the values of cv_o. It does not contain NaN or Inf values. And I am not sure that values of b are right. I had tried calculate the function without using i, j but as you suggested but it did not create b(jj). Could you write me what the matter is?

Community Treasure Hunt

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

Start Hunting!