%testing the elimation of the elements outside the boundary%
np=22;
L=10;
x=linspace(0,L,np);
%position(:,1)=x;
for i=1:np
if x(i) > 5
x(i) = [ ];
end
end
hello, i want to eliminate the value of x greater than 5 , i s not able yo do so. it says index array exceeds array bounds

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 1 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 1 日

2 投票

#No Need of loop, efficient code.
np=22;
L=10;
x=linspace(0,L,np);
idx=find(x>5);
x(idx)=[]
If loop is needed, you can understand the issue from the comment section.

3 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 1 日
using for loop you can undestand the issue from the following example
np=22;
L=10;
x=linspace(0,L,np)
for i=1:length(x)
if x(i)>5
x(i)=NaN
end
end
As @Star explained if you delete the element, the array length reduce, therefore it reflects the error
index array exceeds array bounds
Alchemist
Alchemist 2019 年 7 月 1 日
tnx you sir.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 1 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 1 日
You always welcome ..!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by