a loop that checks for missing decimals in a vector, but it only runs once

2 ビュー (過去 30 日間)
Mads Albertsen
Mads Albertsen 2021 年 8 月 5 日
コメント済み: Mads Albertsen 2021 年 8 月 11 日
if want it to remove the numbers if it doesnt have 1.1, 1.2, 1.3 and so on up til 5 but it only runs once
v = [1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1];
c = [1.1, 1.2, 1.3 ];
y = find(ismember(v,c));
length(y)
for i = length(v)
c = [1.1, 1.2, 1.3 ];
y = find(ismember(v,c));
length(y)
if length(y) < 3
v(y)=[];
c = c+1;
else
c = c+1;
end
end
  1 件のコメント
Rik
Rik 2021 年 8 月 5 日
What are you actually trying to do? Your code is uncommented and your single line description is not clear to me.

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

回答 (1 件)

Awais Saeed
Awais Saeed 2021 年 8 月 5 日
I think you want to delete elemetns of vector c from vector v. If that is what you want then you can do it as shown below
v = [1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1];
c = [1.1, 1.2, 1.3 ];
y = find(ismember(v,c));
for col = length(y):-1:1 % You must do this in reverse order as the size of vector v will keep changing
idx = y(col);
v(idx) = [];
end
  2 件のコメント
Rik
Rik 2021 年 8 月 5 日
If that is the goal you can do it in one go:
v = [1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1];
c = [1.1, 1.2, 1.3 ];
L=ismember(v,c);
v(L)=[]
v = 1×10
2.2000 2.3000 4.2000 5.1000 3.2000 5.3000 3.3000 2.1000 5.2000 3.1000
Mads Albertsen
Mads Albertsen 2021 年 8 月 11 日
i figured it out, was just missing i = 1:length(v) since i need c to check for all the numbers uptil 5, so 4.2 also is removed since it is missing 4.1 and/or 4.3, sorry for being unclear

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by