フィルターのクリア

Index exceeds the number of array elements (1)

3 ビュー (過去 30 日間)
Esila Darci
Esila Darci 2022 年 2 月 9 日
コメント済み: Walter Roberson 2022 年 2 月 10 日
Hello guys,
I have one error in the code "Index exceeds the number of array elements (1)" the code is
jay = [55 : 5 : 85] * 1000
temperature= [0.0002 : 0.0001 : 0.0003]
[n,m]=size(keyur);
for i=1:n
X1=keyur(i,1); X2=keyur(i,2);
X3=0.06;
if X1<X3 && X2>X3
disp([' Successful at ' num2str(jay(i)/1000) ' humidity and temperature ' num2str(temperature(i)*1000)])
end
end
And the error is
Index exceeds the number of array elements (1).
Error in (line 8)
disp([' Successful at ' num2str(jay(i)/1000) ' humidity and temperature' num2str(temperature(i)*1000)])
Any idea to sort it out this error ? thanks ton
  1 件のコメント
Turlough Hughes
Turlough Hughes 2022 年 2 月 9 日
You're trying to access the i'th element in temperature and jay with the index i. However, jay and certainly temperature (which is only a 1 by 3 array) don't have i elements. Hence you get the error: index exceeds the number of array elements.

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

回答 (2 件)

KSSV
KSSV 2022 年 2 月 9 日
編集済み: KSSV 2022 年 2 月 9 日
[n,m]=size(keyur);
jay = linspace(55,85,n)*1000
temperature= linspace(0.0002,0.0003,n) ;
for i=1:n
X1=keyur(i,1); X2=keyur(i,2);
X3=0.06;
if X1<X3 && X2>X3
disp([' Successful at ' num2str(jay(i)/1000) ' humidity and temperature ' num2str(temperature(i)*1000)])
end
end
  3 件のコメント
KSSV
KSSV 2022 年 2 月 9 日
Edited the answer.
Esila Darci
Esila Darci 2022 年 2 月 9 日
What if i don't want to use linespace because i would like to give them set of specific value by putting this condition [min value incremental rate max value] then
sorry but size of keyur value is calculated by at respective jay and temperature value, thats what i used keyur value it into condition.
For exmple
keyur (at jay 55 and temp 0.0002) =
X1 X2
0.00455632639600613 0.0881331282510282
5.45813345811941e-07 0.124865583837929
keyur (at jay 55 and temp 0.0003) =
X1 X2
0.00555632639600613 0.0251331282510282
2.45813345811941e-07 0.134865583837929
keyur (at jay 60 and temp 0.0002) =
X1 X2
0.00456632639600613 0.0681331282510282
2.45813345811941e-07 0.324865583837929
and so on different keyur value according to jay and temperature ...............
Thanks

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


Walter Roberson
Walter Roberson 2022 年 2 月 9 日
format long g
jay = [55 : 5 : 85] * 1000
jay = 1×7
55000 60000 65000 70000 75000 80000 85000
temperature= [0.0002 : 0.0001 : 0.0003]
temperature = 1×2
1.0e+00 * 0.0002 0.0003
Your jay will definitely have more than one value. However, depending exactly how you calculated the bounds for temperature, temperature might end up with only a single element. You are hoping that 0.0002 + 0.0001 <= 0.0003 but that is not certain in MATLAB:
.1 + .2 - .3
ans =
5.55111512312578e-17
Notice this is not exactly 0. You have round-off going on, because 1/10^N is not exactly representable in IEEE 754 double precision for any positive integer N. -- not 1/100, not 1/1000 and so on. Sometimes the round-off falls the way you would like, but sometimes it doesn't. Depending exactly how you calculated the 0.0002 and 0.0003 they might have ended up slightly less than 0.0001 apart and so temperature might have been just the starting value.
[n,m]=size(keyur);
for i=1:n
fundamental problem there: the size of keyur is not related to the size of jay or the size of temperature, but you assume that you can index both according to the number of rows in keyur .
  7 件のコメント
Image Analyst
Image Analyst 2022 年 2 月 9 日
You should then replace
keyur = keyur(this_jay, this_temperature);
with
keyur = Keyur(this_jay, this_temperature);
however I think it's bad practice to call your returned variable almost the same name as your function, despite the fact that MATLAB is case sensitive and Keyur is different than keyur. When you use keyur instead of Keyur, you're telling it you want the this_jay'th row and this_temperature'th column of the array keyur instead of telling it to pass those two values into the Keyur() function.
I recommend you either use a different name for keyur, like computedKeyur, or use a different name for your Keyur() function, like ComputeKeyur(), to avoid future confusion.
Walter Roberson
Walter Roberson 2022 年 2 月 10 日
I agree with Image Analyst that it is best to use a variable name that is obviously different than the function name.
I personally have been known to use upper case and lower case variables that are otherwise the same; in particular I personally tend towards the practice of using lower case to represent vectors of values and upper case to represent grids of values (output from meshgrid() or ndgrid()) .
However, I mess up sometimes myself, and it can be difficult to catch. And I have certainly seen a number of times when other people have messed up when using different variables with only upper / lower distinction... and a lot of those times, it can be difficult to repair. People writing code tend to get confused between an entire array, and a variable representing a "current" value being paid attention to out of the array. When that happens they tend to write code that assumes that somehow at each point the "current" value is being updated as needed... often with different intended "current" values at different points in the same expression. Using obviously different variable names helps to focus the attention of the programmer to not confuse the individual element with the whole array.

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

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by