如何以时间有效的方式清除部分向量?

use*_*276 1 matlab octave

如何以时间有效的方式参与矢量并用零填充其他部分?

我有一个矢量;

 A  |  |                               A  | 0 |               
    |  |                                  | 0 |
    |  |                                  | 0 |
    |  | <-------- x         -----------> |   |  <--|
    |  |                                  |   |     |
    |  |                                  |   |     |     data in this region
    |  |                    after         |   |     |      is not changed
    |  |                                  |   |     |     
    |  |                  operation       |   |     |
    |  |                                  |   |     |
    |  |                                  |   |  <--|
    |  | <--------- y        -----------> |   |
    |  |                                  | 0 |
    |  |                                  | 0 |
    |  |                                  | 0 |
Run Code Online (Sandbox Code Playgroud)

Sup*_*est 7

u = 1:10;
v = [1:3, 8:10];

u(v) = 0;
Run Code Online (Sandbox Code Playgroud)

将第一个和最后三个元素设置为零.为了使它更类似于你如何表达问题:

x = 3;
y = 8;
u = 2:2:20;
v = x:y;
w = 1:length(u);

u(setdiff(w, v)) = 0;
Run Code Online (Sandbox Code Playgroud)

虽然您可能更愿意这样做:

u(1:x-1) = 0;
u(y+1:end) = 0;
Run Code Online (Sandbox Code Playgroud)

(仅当您想要包含范围时,+/ - 1才会出现.)