matlab - matlab中元胞数组元素的线性索引

标签 matlab indexing cell

考虑以下元胞数组:

A={1:3,[20,40],100}

A =

1×3 cell array

   {1×3 double}    {1×2 double}    {[100]}

我想要一种方法来检索存储它的值的​​线性索引,例如,如果我使用以下方法展平数组:

[A{:}]

ans =

     1     2     3    20    40   100

我可以看到第 4 个线性索引是 20 等等。那么有没有一种类似于矩阵线性索引的方法可以给我一个元胞数组 A((4)) 示例中的值 20? (当然,我只是出于说明目的发明了 (()) 符号。

最佳答案

据我所知,没有直接的解决方案。这是实现这一目标的方法。即使内部数组不是行向量,它也能工作;在那种情况下,它们被隐含地认为是线性化的。

A = {1:3,[20,40],100}; % data
ind = 4; % linear index into flattened data
s = [0 cumsum(cellfun(@numel, A))];
ind_outer = find(ind<=s, 1) - 1;
ind_inner = ind - s(ind_outer);
result = A{ind_outer}(ind_inner);

关于matlab - matlab中元胞数组元素的线性索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49288465/

相关文章:

performance - 在 matlab 中运行编译的 mex 代码时,mingw 是否比 cygwin 慢?

c - 将 matlab 脚本转换为 C 程序的移动平均线

python - 用户警告 : Pandas doesn't allow columns to be created via a new attribute name

mysql - UNION 查询的索引?

c - 执行错误: Part of C code compiled but not being executed properly in ECM

python - 有没有办法通过Python中的正则表达式找到子字符串索引?

html - CSS 单元格边距

matlab - 将 DICOM 数据读入元胞数组的性能问题

excel - 提取Excel单元格中的子字符串

matlab - 如何让子字符串在 matlab 中工作?