Is there a way to use find function if I know there will be some empty sets?

4 ビュー (過去 30 日間)
Andrew Poissant
Andrew Poissant 2017 年 8 月 30 日
編集済み: Stephen23 2017 年 8 月 31 日
I am using matlab's 'find' function below. I get an error saying "Subscripted assignment dimension mismatch," because there are some empty sets in the find solution. I know that 'find' will return an error if it cannot find a solution, but is there a way around this? I simply want a way to use 'find' (or something equivalent) that will not give an error when no sol is found but will instead give an empty set. A simplified version of my code is below where N is a layered 824x824x3 matrix and FGIF is a 936x2 matrix.
[FGIF_r, FGIF_c] = size(FGIF);
tol = 0.00005;
for i = 1:FGIF_r
[row(i), col(i)] = find(abs(N_new(:,:,2) - FGIF(i,1)) <= tol & abs(N_new(:,:,3) - FGIF(i,2)) <= tol);
end
  2 件のコメント
Jan
Jan 2017 年 8 月 30 日
I know that 'find' will return an error if it cannot find a solution
No, FIND does not cause an error; when it cannot find a match. It simply replies and empty matrix.
Stephen23
Stephen23 2017 年 8 月 31 日
編集済み: Stephen23 2017 年 8 月 31 日
"I know that 'find' will return an error if it cannot find a solution"
Where is that error? I cannot see any error occurring. If find is throwing an error then there is something seriously wrong with your MATLAB installation.
It is not find that has any problems, but your algorithm. So fix your algorithm to correctly handle cases with empty, scalar, and non-scaler outputs from find.
" I simply want a way to use 'find' ... that will not give an error when no sol is found but will instead give an empty set"
>> find(0)
ans = []

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

回答 (2 件)

Image Analyst
Image Analyst 2017 年 8 月 30 日
You could use isempty() to check and do stuff if it's valid and skip it or do other stuff if it's empty.
for i = 1:FGIF_r
expression = abs(N_new(:,:,2) - FGIF(i,1)) <= tol & abs(N_new(:,:,3) - FGIF(i,2)) <= tol;
if ~isempty(expression)
[row(i), col(i)] = find(expression);
else
uiwait(warndlg('Expression is empty'));
% Do whatever you need to do if it's empty....
end
end

Eng. Fredius Magige
Eng. Fredius Magige 2017 年 8 月 30 日
Hi modify and use switch case, as go the second if the first fond empty set ([ ]) Thanks
  1 件のコメント
Andrew Poissant
Andrew Poissant 2017 年 8 月 30 日
Could you please explain a little more of what you are talking about? I am confused about your response.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by