matlab - 以独特的方式平铺向量 MATLAB

标签 matlab vectorization tiling run-length-encoding

考虑 A = [ 200000 x 1] 向量。我有另一个向量idx = [200000x1]。我想平铺A,使得A的每个ith<​​元素都平铺idx(i)次。

例如:

A   idx output 
2   2    2
3   1    2
4   3    3 
5   1    4
.   .    4
.   .    4
.   .    5 
.    .     .

有什么非循环的想法吗?

最佳答案

您似乎正在寻找 run length decoding :即 idx(ii) 表示长度 A(ii) 应出现在编码输出中。

这是在 Matlab 中执行此操作的好方法:

output = zeros(1, sum(idx)); % allocate output
output( cumsum( [1 idx(1:end-1)] ) ) = 1;
output = A( cumsum( output ) );

output =

 2     2     3     4     4     4     5

关于matlab - 以独特的方式平铺向量 MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503014/

相关文章:

c++ - 为什么 MATLAB/Octave 在特征值问题中用 C++ 擦地板?

windows - 如何在 Matlab 中停止正在运行的脚本

r - 对具有后向依赖性的 R 循环进行向量化

c++ - 缓存敏感矩阵乘法的多级平铺

algorithm - Matlab 中的离散傅里叶变换 - 理论困惑

matlab - 包含 MATLAB 图轴的框中缺少黑线

从同一行中指示的列返回值

python-3.x - Pandas 使用除 apply 之外的许多条件添加新列的替代方法

algorithm - 数学生成球形六角网格

android - 平铺的drawable有时会延伸