matlab - 创建向量

标签 matlab vector indexing vectorization

输出向量应如下所示:

a=[3 3 3 4 4 4 4 5 5 5 5 5]

我拥有的是:

pe=[1 5 9] and ne=[4 8 12] and co=[3 4 5]

pe 描述每个条目的起始索引和结束索引,co 描述该条目的值

我想在没有循环的情况下执行此操作。 使用循环它应该看起来像这样:

  for i=1:3
     a(pe(i):ne(i))=co(i)
   end

最佳答案

实现此目的的一种方法是首先使用 cumsum 创建一个索引数组到 co

idxList = zeros(1,max(ne)); %# create an array with zeros
idxList(pe) = 1;            %# mark the start of a new index
idxList = cumsum(idxList);  %# now, idxList has 1's where we should
                            %# place the first element of co, etc
out = co(idxList);          %# and we're done. 

关于matlab - 创建向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610207/

相关文章:

java - 使用Java对象模仿Matlab结构

matlab - 如何在 Matlab 中的 CIE 1931 色彩空间上绘制颜色?

c++ - 访问 vector 时中止

Mysql多列索引使用错误的索引

java - Neo4j 索引(使用 Lucene)- 组织节点 "types"的好方法?

matlab - 以特定方式 reshape MATLAB 矩阵

matlab - 使用 MATLAB 对基于相机的图像进行 OMR

loops - 为什么 Vector3 不更新 Unity3d

c++ - 获取段错误( vector 声明)

indexing - Julia - 将 CartesianIndices 与数组一起使用