Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:如何在向量的特定位置插入零_Matlab_Vector_Insert - Fatal编程技术网

Matlab:如何在向量的特定位置插入零

Matlab:如何在向量的特定位置插入零,matlab,vector,insert,Matlab,Vector,Insert,有人能帮我解决Matlab中的以下问题吗?我有一个包含元素值的第一个向量。比如说, [2 8 4 9 3]. [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1]. 以及在第二向量中具有所需位置的第二向量。比如说, [2 8 4 9 3]. [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1]. 现在我想把第一个向量的值放在第二个向量的位置上,以 [0 0 2 0 0 0 0 8 4 0 0 9 0 0 3]. 当向量的大小可能非常大时,最有效的方法是什么

有人能帮我解决Matlab中的以下问题吗?我有一个包含元素值的第一个向量。比如说,

[2 8 4 9 3]. 
[0 0 1 0 0 0 0 1 1 0 0 1 0 0 1]. 
以及在第二向量中具有所需位置的第二向量。比如说,

[2 8 4 9 3]. 
[0 0 1 0 0 0 0 1 1 0 0 1 0 0 1]. 
现在我想把第一个向量的值放在第二个向量的位置上,以

[0 0 2 0 0 0 0 8 4 0 0 9 0 0 3]. 

当向量的大小可能非常大时,最有效的方法是什么。(然后是数千个元素)

您可以将y值视为逻辑指示器,然后使用逻辑索引将这些值设置为x中的值

x = [2 8 4 9 3];
y =  [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1];
y(logical(y)) = x;
或者,您可以使用

y(y==1) = x;

您可以将y值视为逻辑指示符,然后使用逻辑索引将这些值设置为x中的值

x = [2 8 4 9 3];
y =  [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1];
y(logical(y)) = x;
或者,您可以使用

y(y==1) = x;
使用自索引:

% Your values:
V = [2 8 4 9 3];

% The desired locations of these values:
inds = [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1];

% index the indices and assign
inds(inds>0) = V
使用自索引:

% Your values:
V = [2 8 4 9 3];

% The desired locations of these values:
inds = [0 0 1 0 0 0 0 1 1 0 0 1 0 0 1];

% index the indices and assign
inds(inds>0) = V