Why is this generating a numerical array and not an array of cells?

1 回表示 (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2022 年 3 月 17 日
コメント済み: Image Analyst 2022 年 3 月 17 日
Why does the following loop generate slope as the result of diff() function? Instead or a cell array with the desired text?
for i=1:length(Temp_F)
if diff(Temp_F(i))>0
slope(i)='Rising'
elseif diff(Temp_F(i))<0
slope(i)='Falling'
end
end
  1 件のコメント
Stephen23
Stephen23 2022 年 3 月 17 日
diff(Temp_F(i))
What do you expect the difference of one number to be?

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

採用された回答

KSSV
KSSV 2022 年 3 月 17 日
REad about the function diff. It needs an array with atleast 1x2 dimensions. You are giving only one number as input. So diff gives you an empty matrix.
diff(1)
ans = []
diff(rand)
ans = []
diff(rand(1,2))
ans = 0.0347
diff(rand(2,1))
ans = 0.5240
So you have to modify your code like below:
diff_Temp_F = diff(Temp_F) ; % I assume Temp_F is a vector
N = length(Tmp_F) ;
slope = cell(N,1) ;
for i=1:length(Temp_F)-1
if diff_Temp_F(i) > 0
slope{i}='Rising'
elseif diff_Temp_F(i) < 0
slope{i}='Falling'
end
end
  1 件のコメント
Image Analyst
Image Analyst 2022 年 3 月 17 日
I believe you could also use strings instead of character arrays and then use parentheses instead of braces
slope(i) = "Rising"; % Use double quotes instead of single quotes.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by