如何以省时的方式清除向量的一部分? [英] how to clear part of vector in time efficient way?
本文介绍了如何以省时的方式清除向量的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以省时的方式获取向量的一部分并用零填充其他部分?
How can I take part of the vector and fill other part with zero in time efficient way ?
我有一个向量;
A | | A | 0 |
| | | 0 |
| | | 0 |
| | <-------- x -----------> | | <--|
| | | | |
| | | | | data in this region
| | after | | | is not changed
| | | | |
| | operation | | |
| | | | |
| | | | <--|
| | <--------- y -----------> | |
| | | 0 |
| | | 0 |
| | | 0 |
推荐答案
u = 1:10;
v = [1:3, 8:10];
u(v) = 0;
将前3个元素和后3个元素设置为零.为了使其与您对问题的表述方式更加相似:
Will set the first and last 3 elements to zero. To make it more similar to how you phrased the question:
x = 3;
y = 8;
u = 2:2:20;
v = x:y;
w = 1:length(u);
u(setdiff(w, v)) = 0;
尽管您可能更愿意这样做:
Although you would probably prefer to just do:
u(1:x-1) = 0;
u(y+1:end) = 0;
(仅当您想要包含范围时,才+/- 1.)
(The +/-1 is only if you want inclusive range.)
这篇关于如何以省时的方式清除向量的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文