First value of possible empty array

7 ビュー (過去 30 日間)
K VdB
K VdB 2016 年 10 月 6 日
編集済み: Adam 2016 年 10 月 6 日
I need to find the first value in an array. Simple:
A = [1.2, 2.3, 3.4, 4.5, 5.6]
A(1) = 1.2
But sometimes this array can be an empty array: A = []
Then the code gives an error. I want it to give: A(1) = [];
Is this possible without the: -if isempty(A)==0- command?
greetings, K

回答 (1 件)

Adam
Adam 2016 年 10 月 6 日
編集済み: Adam 2016 年 10 月 6 日
if isempty(A)
works just as well as
if isempty(A) == 0
which is redundant.
If your array is empty though then you will need to handle this as such. You cannot index into an empty array.
In many usages you would want to make this check anyway though because your array being empty is a significant difference from it not being empty so you would expect to run different code in such circumstances.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by