Using find in a for loop error

12 ビュー (過去 30 日間)
Serena Campbell
Serena Campbell 2018 年 2 月 12 日
回答済み: Serena Campbell 2018 年 2 月 12 日
I have a large matrix with values of "period" p - I am testing this part of my code where I find what row p belongs to in order to access the corresponding row later in my code. However for some reason this will only run three times (so until cct=3) before i get the error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts; error position1(cct,1)=find(p==pp)
*
p=AAA(1:end-1,1); %%%returns 10000x1 matrix of values 0 to 1 in 0.001 steps
cct=1;
for pp =0.1:0.1:0.5;
position1(cct,1)=find(p==pp);
position2=position1(cct,1);
valuep=p(position2,1); %%%to check it is the correct value
cct=cct+1;
end*
  1 件のコメント
Serena Campbell
Serena Campbell 2018 年 2 月 12 日
編集済み: Serena Campbell 2018 年 2 月 12 日
So - I put the output into a cell and a strange thing happens - it always skips the third value (no matter what the value is!) and continues nicely after? (see attached output)
p=AAA(1:end-1,1);
cct=1;
for pp =0.4:0.1:1;
test(cct,1)=cct;
valuepp(cct,1)=pp;
x{cct,1}=find(p==pp);
%
cct=cct+1;
end

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

採用された回答

Matt J
Matt J 2018 年 2 月 12 日
編集済み: Matt J 2018 年 2 月 12 日
You would need to give us AAA in a .mat file to be certain. Most likely, however, pp is not close enough to 0.6 (due to floating point error) to match a value in AAA. Use a tolerance:
x{cct,1}=find(abs(p-pp)<smallnumber);

その他の回答 (1 件)

Serena Campbell
Serena Campbell 2018 年 2 月 12 日
I worked it out! It must have been a precision problem, as this fixed it:
https://uk.mathworks.com/matlabcentral/answers/17534-find-function-precision-problem

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by