How can I use an empty matrix in a logical if statement?

64 ビュー (過去 30 日間)
K
K 2014 年 1 月 22 日
回答済み: Jackson 2017 年 7 月 20 日
Hello,
I have a program that evaluates mode shapes using the null space of an 8X8 matrix, "K". It works great when the null space exists, and I get an 8X1 vector for "u". However, for some values of "K" there is no null space, so for that "u" I get "Empty matrix: 8-by-0" and need to use a different method.
I tried to use
if eval(['u',num2str(ii),'==[]'])
to test if u is an empty matrix, but I get an error that tells me that "Matrix dimensions must agree". Is there any way to use an if statement that will run only if u is an empty 8x0 matrix?
(And yes, I know that eval + num2str is frowned upon, I didn't write that part! Apologies in advance!)
Thanks! Kaitlin

採用された回答

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014 年 1 月 22 日
編集済み: Bruno Pop-Stefanov 2014 年 1 月 22 日
Use the isempty function to check if an array is empty:
if isempty(u)
% do something
else
% do something else
end
  1 件のコメント
K
K 2014 年 1 月 22 日
Thank you! That worked- thanks for directing me to the right command!

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

その他の回答 (1 件)

Jackson
Jackson 2017 年 7 月 20 日
When given a numeric array, if statements look at the first element and return true if it is non-zero. If the array is empty then this appears to be interpreted as false. Run the following:
if []
disp TEST1
end
if 0 : 10
disp TEST2
end
if 1 : 10
disp TEST3
end
a = [] ;
b = 1 : 10 ;
if a
disp TEST4
elseif b
disp TEST5
end
And only TEST3 and TEST5 are printed on the command line.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by