【问题标题】:Extracting elements from matrix using strings subscript error使用字符串下标错误从矩阵中提取元素
【发布时间】:2015-03-25 13:27:13
【问题描述】:

您好,我有一个庞大的 Excel 表格,我想提取特定化合物的值。矩阵在 72 个街区中重复出现,其中车辆类别在不同的道路坡度、化合物和驾驶条件下重复。

    cb=[Vehicleclass_raw,Gradient_raw,Component_raw];
    size cb= 119952x3 cell
    Vehicleclass_airquis is string with 37 elements 

所以我想我根据标准渐变车辆类和组件对其进行排序,这就是代码打击。它适用于前 30 个元素,然后它崩溃并且错误消息是下标分配维度不匹配。 outval_aq(i,:) = (行)。我无法弄清楚错误是什么。感谢马蒂亚斯的帮助

    outval_aq = ones(37,72)*119953;
    for i=1:37
    rows = find(strcmp(cb(:,1),Vehclass_airquis(i)) & strcmp(cb(:,2),'0%') &     strcmp(cb(:,3),'NOx'));
    if ~isempty(rows)
    outval_aq(i,:) = (rows)
    end
    end

【问题讨论】:

  • 错了,为什么行是72个数字向量?
  • 当它崩溃时rows 是什么?
  • 行大小为 60 且 i=34

标签: excel matlab strcmp


【解决方案1】:

在矩阵中,每一行都有相同数量的元素。在您的情况下,您将 outval_aq 初始化为 37x72 矩阵,但 rows 只有 60 个元素。解决方案可能如下所示:

%pad rows with nans to make it the required size:
rows(end+1:72)=nan
outval_aq(i,:) = (rows)

.

%write only the first 60 elements to the matrix, leave the remaining untouched:
outval_aq(i,1:numel(rows)) = (rows)

或者您可以将 outval_aq 更改为包含向量的元胞数组。

【讨论】:

  • 第二部分超出了val_aq(i,1:numel(rows)) = (rows) :-) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 2021-06-13
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
相关资源
最近更新 更多