如何使MATLAB重复多个向量的元素? [英] How to make elements of multiple vectors repeated by MATLAB?

查看:1210
本文介绍了如何使MATLAB重复多个向量的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据向量 d1 = [1 2 1 3 4] d2 = [3 1 2 1] 和2个引用 howMany1 = [3 2 2 1 2] howMany2 = [2 1 2 2] 。 d1和d2的元素根据howMany1和howMany2的元素进行重复:所以d1(1)即d1的第一个元素应该重复3次,d1(2)应该重复2次,依此类推。最终结果应该是 d1_repeated = [1 1 1 2 2 1 1 3 4 4] d2_repeated = [3 3 1 2 2 1 1]



我怎么能在MATLAB中做到这一点?我回顾了一个类似的帖子,其中一个向量正在重复,所以我试图做同样的并做了一个for循环。这是我的代码:

 清除所有
关闭所有
clc
%data

d1 = [1 2 1 3 4];
d2 = [3 1 2 1];
howMany1 = [3 2 2 1 2]; %确定IDX1中的每个索引应重复多少次。
howMany2 = [2 1 2 2];
d = {d1 d2}
howMany = {howMany1 howMany2}
Anew = cell(1,2)
for k = 1:2%2数据向量
A new {1,k} = arrayfun(@(x)repmat(d {k}(x),howMany {k}(x),1),1:numel(d {k}),'uni',0)
Anew = vertcat(A new {:});

end

但我收到以下错误:

 使用vertcat的错误

连接的矩阵的大小不一致。

然后我试着把hor猫改成horzcat,但是我收到了重复:

  Anew = 

第1列到第7列

[3x1双倍] [2x1 double] [1] [2x1 double] [2x1 double] [2x1 double] [3]

第8列

[2x1 double]

我想知道这里有什么问题?感谢您花时间。

解决方案

这可以通过组合ismember和cumsum来完成:

  d1(cumsum(ismember(1:sum(howMany1),cumsum([1 howMany1])))


细分 :sum(howMany1) howMany1 的累计和。这是一个带有1和0的向量,表示从d1开始下一个值的位置。

  ismember(1:sum(howMany1),cumsum([1 howMany1]))
ans =
1 0 0 1 0 1 0 1 1 0

这个累积和给出了一个向量,例如:
$ b $ pre $ cum codeum $ sum $ ans =
1 1 1 2 2 3 3 4 5 5

现在,这可以在创建d1_repeated时用作 d1 的索引。

  d1(cumsum (ismember(1:sum(howMany1),cumsum([1 howMany1])))
ans =
1 1 1 2 2 1 1 3 4 4


I have 2 data vectors d1 = [1 2 1 3 4] and d2 = [3 1 2 1], and 2 references howMany1= [3 2 2 1 2] and howMany2 = [2 1 2 2]. The elements of d1 and d2 are to be repeated according to the elements of howMany1 and howMany2: so d1(1) i.e. the first element of d1 should repeat 3 times,d1(2) should repeat 2 times and so on.. and the final result should be d1_repeated = [1 1 1 2 2 1 1 3 4 4] and d2_repeated = [3 3 1 2 2 1 1]

How could I do that in MATLAB? I reviewed the a similar post in which one vector is being repeated, so I tried to do the same and made a for loop. Here is my code:

clear all
close all
clc
% data

d1 = [1 2 1 3 4];
d2 = [3 1 2 1];
howMany1 = [3 2 2 1 2]; % Determines how many times each index in IDX1 should be repeated.
howMany2 = [2 1 2 2];
d = {d1 d2}
howMany = {howMany1 howMany2}
Anew = cell(1,2)
for k = 1:2 % 2 data vectors     
Anew{1,k} = arrayfun(@(x) repmat(d{k}(x), howMany{k}(x), 1), 1:numel(d{k}), 'uni', 0);
Anew = vertcat(Anew{:});

end

but I receive the following error:

Error using vertcat

Dimensions of matrices being concatenated are not consistent.

Then I tried to change the vert cat to horzcat, but I receive the repetitions as group of doubles:

Anew = 

  Columns 1 through 7

    [3x1 double]    [2x1 double]    [1]    [2x1 double]    [2x1 double]    [2x1 double]    [3]

  Column 8

    [2x1 double]

Im wondering what is the problem here? Thank you for taking the time.

解决方案

This can be done by combining ismember and cumsum:

d1(cumsum(ismember(1:sum(howMany1),cumsum([1 howMany1]))))

Breakdown:

Compare 1:sum(howMany1) with the cumulative sum of howMany1. This is a vector with 1 and 0 that shows the position of where the next value from d1 should start.

ismember(1:sum(howMany1),cumsum([1 howMany1]))
ans =
     1     0     0     1     0     1     0     1     1     0

The cumulative sum of this gives a vector that looks like:

cumsum(ismember(1:sum(howMany1),cumsum([1 howMany1])))
ans =
     1     1     1     2     2     3     3     4     5     5

Now, this can be used as indices to d1 when creating the d1_repeated.

d1(cumsum(ismember(1:sum(howMany1),cumsum([1 howMany1]))))
ans =
     1     1     1     2     2     1     1     3     4     4

这篇关于如何使MATLAB重复多个向量的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆