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

1 回表示 (過去 30 日間)
Szabó-Takács Beáta
Szabó-Takács Beáta 2015 年 4 月 20 日
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?

採用された回答

Michael Haderlein
Michael Haderlein 2015 年 4 月 20 日
編集済み: Michael Haderlein 2015 年 4 月 20 日
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 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 4 月 21 日
To keep the thread readable, I copy-paste your post here. Please also answer as comment here. The sequence of answers might change and the thread will be confusing then.
Answer by Szabó-Takács Beáta:
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?
=========== my response ==============
I had another look on your function, why do you actually use fzero at all? You are using element-wise operators (with dot), so my analytical solution from the above answer holds, too. You can simplify your code to
for jj=1:360;
mat=cv_o{jj};
mstd=std_m{jj};
mmean=mean_m{jj};
BB{jj}=log(mat)./log(mstd./mmean);
BB{jj}(mat==0)=0;
end
In principle, by using cellfun you could even cut it down to a one-liner without any loop (I was using rand values for cv_o, std_m, mean_m; also please note the transpose operator needed to show equality):
>>PP=cellfun(@(x,y,z) log(x)./log(y./z), cv_o, std_m, mean_m,'uniform',false);
>> isequal(PP',BB)
ans =
1
With respect to the error message, maybe mmean is zero somewhere. Then your function is inf (or NaN if the same mstd is also zero) and causes the error.
Szabó-Takács Beáta
Szabó-Takács Beáta 2015 年 4 月 22 日
Thank you! Meantime I tried the code you that suggested and it works.

サインインしてコメントする。

その他の回答 (1 件)

Szabó-Takács Beáta
Szabó-Takács Beáta 2015 年 4 月 21 日
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?

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by