a loop that checks for missing decimals in a vector, but it only runs once
2 次查看(过去 30 天)
显示 更早的评论
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
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
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
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)=[]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!