フィルターのクリア

Help me avoid this error: Empty matrix: 0-by-1

3 ビュー (過去 30 日間)
Pham
Pham 2013 年 8 月 5 日
I have a program like this:
clc
a=[0 0; 150 0.5; 300 0.35; 450 0.253; 600 0.22; 750 0.276; 900 0.183; 1050 0.228; 1200 0.17; 1350 0.207; 1500 0.2; 1650 0.19; 1800 0.187; 1950 0.21; 2100 0.176; 2250 0.197; 2400 0.17]
b=[900 900 750 2100 750 150 150 150 300]'
for i=1:5
c=find(a(:,1)==b(i));
f=a(c,:);
d(i)=prod(f);
end
e=sum(d)
My main program for many vectors b with components of a. I want to find e, but few result of c are Empty matrix: 0-by-1
Help me. Thanks.
  1 件のコメント
Pham
Pham 2013 年 8 月 5 日
編集済み: Pham 2013 年 8 月 5 日
Thank you for helping me. But I do not understand why in my main program, with 3 value "150 150 150" in vector b, then the vector c is "0.5 Empty matrix: 0-by-7 Empty matrix: 0-by-8", without "0.5 0.5 0.5" ??

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

採用された回答

Iain
Iain 2013 年 8 月 5 日
編集済み: Iain 2013 年 8 月 5 日
Change : d(i) = prod(f); to
if isempty(f)
d(i) = NaN;
else
d(i) = prod(f);
end
and e = sum(d) to:
e = nansum(d);
If NaN isn't convenient, change it for something that is.

その他の回答 (1 件)

Jan
Jan 2013 年 8 月 5 日
d = nan(1, 5);
for i=1:5
c = (a(:, 1) == b(i));
if any(c)
d(i) = prod(a(c, :));
end
end
  1 件のコメント
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis 2019 年 5 月 25 日
Thank you @Jan, you have helped me

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by