【问题标题】:Add the same value to multiple vectors in Matlab将相同的值添加到Matlab中的多个向量
【发布时间】:2016-10-26 14:05:04
【问题描述】:

我有一个 Matlab 脚本,如下所示:

onsets=cell(1,5);
onsets{1}=[353.1    389.9   404 431.3];
onsets{2}=[369.8    385.8   427.7];
onsets{3}=[344.6    359.7   397.5];
onsets{4}=[364.7    415.1   420.1];
onsets{5}=[146  174.6   204.2]; 

我想将 2.24 添加到所有向量(现在我正在逐个向量进行)并获得如下所示的输出:

onsets=cell(1,5);
onsets{1}=[355.3400  392.1400  406.2400  433.5400];
onsets{2}=[372.0400  388.0400  429.9400];
onsets{3}=[346.8400  361.9400  399.7400];
onsets{4}=[346.8400  361.9400  399.7400];
onsets{5}=[366.9400  417.3400  422.3400]; 

什么是更快更有效的方法?

【问题讨论】:

    标签: matlab variables vector add


    【解决方案1】:

    您可以使用cellfun 将操作应用于所有单元格内容:

    result = cellfun(@(x) x+2.24, onsets, 'UniformOutput', false);
    

    【讨论】:

    • 我猜这是最自然的单行解决方案,但请注意,这与在单元格数组上循环并在循环的每个步骤中添加 2.24 并没有太大区别。 -- 我想说,就用你觉得最易读的吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多