Matlab 中的逐元素数组复制

发布于 2024-08-15 16:38:55 字数 348 浏览 9 评论 0原文

假设我有一个一维数组: <代码>

a = [1, 2, 3];

是否有一个内置的 Matlab 函数,它接受一个数组和一个整数 n 并复制每个 数组元素n次?

例如,调用 replicate(a, 3) 应返回 [1,1,1,2,2,2,3,3,3]

请注意,这与 repmat 完全不同。我当然可以通过对每个元素执行 repmat 并连接结果来实现 replicate,但我想知道是否有一个更高效的内置函数。

Let's say I have a one-dimensional array:

a = [1, 2, 3];

Is there a built-in Matlab function that takes an array and an integer n and replicates each
element of the array n times?

For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3].

Note that this is not at all the same as repmat. I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吾性傲以野 2024-08-22 16:38:55

我是 KRON 函数的粉丝:

>> a = 1:3;
>> N = 3;
>> b = kron(a,ones(1,N))

b =

    1     1     1     2     2     2     3     3     3

您还可以查看 此相关问题(涉及复制二维矩阵的元素)以查看涉及矩阵索引的其他一些解决方案。这是一个这样的解决方案(灵感来自 Edric 的回答):

>> b = a(ceil((1:N*numel(a))/N))

b =

    1     1     1     2     2     2     3     3     3

I'm a fan of the KRON function:

>> a = 1:3;
>> N = 3;
>> b = kron(a,ones(1,N))

b =

    1     1     1     2     2     2     3     3     3

You can also look at this related question (which dealt with replicating elements of 2-D matrices) to see some of the other solutions involving matrix indexing. Here's one such solution (inspired by Edric's answer):

>> b = a(ceil((1:N*numel(a))/N))

b =

    1     1     1     2     2     2     3     3     3
猫瑾少女 2024-08-22 16:38:55
a = [1 2 3];
N = 3;

b = reshape(repmat(a,N,1), 1, [])
a = [1 2 3];
N = 3;

b = reshape(repmat(a,N,1), 1, [])
温柔女人霸气范 2024-08-22 16:38:55

从 R2015a 开始,有一个内置和记录了执行此操作的函数,repelem

repelem 复制数组的元素。
   W = repelem(V,N),使用向量 V 和标量 N,创建向量 W,其中 V 的每个元素都重复 N 次。

第二个参数也可以是与 V 长度相同的向量,以指定每个元素的复制次数。对于 2D 复制:

B = repelem(A,N1,N2)

不再需要 kron 或其他技巧!

更新:要与其他快速方法进行性能比较,请参阅问答重复数组元素的副本: MATLAB 中的游程解码

As of R2015a, there is a built-in and documented function to do this, repelem:

repelem Replicate elements of an array.
    W = repelem(V,N), with vector V and scalar N, creates a vector W where each element of V is repeated N times.

The second argument can also be a vector of the same length as V to specify the number of replications for each element. For 2D replication:

B = repelem(A,N1,N2)

No need for kron or other tricks anymore!

UPDATE: For a performance comparison with other speedy methods, please see the Q&A Repeat copies of array elements: Run-length decoding in MATLAB.

因为看清所以看轻 2024-08-22 16:38:55
>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)

ans =

     1     1     1     2     2     2     3     3     3
>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)

ans =

     1     1     1     2     2     2     3     3     3
傲鸠 2024-08-22 16:38:55

一些奇异的替代品。诚然,有趣多于有用:

  1. meshgrid 的(第一个)结果分配给向量:

    b = NaN(1,numel(a)*n); %//预整形结果
    b(:) = meshgrid(a,1:n);
    
  2. 构建一个矩阵,乘以 a 得到结果:

    b = a * Fliplr(sortrows(repmat(eye(numel(a)),n,1))).';
    
  3. 使用 ind2sub 生成索引:

    [~, ind] = ind2sub([n 1],1:numel(a)*n);
    b = a(ind);
    

Some exotic alternatives. Admittedly more funny than useful:

  1. Assign the (first) result of meshgrid to a vector:

    b = NaN(1,numel(a)*n); %// pre-shape result
    b(:) = meshgrid(a,1:n);
    
  2. Build a matrix that multiplied by a gives the result:

    b = a * fliplr(sortrows(repmat(eye(numel(a)),n,1))).';
    
  3. Use ind2sub to generate the indices:

    [~, ind] = ind2sub([n 1],1:numel(a)*n);
    b = a(ind);
    
心的位置 2024-08-22 16:38:55

如果您有图像处理工具箱,还有另一种选择:

N = 3;
imresize(a, [1 N*numel(a)],'nearest')

If you have the image processing toolbox, there is another alternative:

N = 3;
imresize(a, [1 N*numel(a)],'nearest')
居里长安 2024-08-22 16:38:55
% To get b = [1 1 1 2 2 2 3 3 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a,1,numel(temp_a));

% To get b = [1 2 3 1 2 3 1 2 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a',1,numel(temp_a));
% To get b = [1 1 1 2 2 2 3 3 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a,1,numel(temp_a));

% To get b = [1 2 3 1 2 3 1 2 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a',1,numel(temp_a));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文