【问题标题】:extracting out elements from a vector in matlab [duplicate]从matlab中的向量中提取元素[重复]
【发布时间】:2013-09-17 13:50:56
【问题描述】:

我有一个 1x600 (rowsxcolumns) 向量,比如“A”,我想在其中访问 A 的以下列:

166   (column value is 2)
256   (column value is 5)
346   (column value is 8)
436   (column value is 10)
526   (column value is 13)

提取出这些列后,我想添加它们各自的值:

sum = 2 + 5 +8 + 10 + 13

任何人都可以帮助我了解如何首先提取列,然后总结它们的值吗?谢谢!

【问题讨论】:

标签: matlab


【解决方案1】:

使用

idxToSum = [166 256 346 436 526]; % or another way to give find your indices
yourSum = sum(A(idxToSum));

【讨论】:

    【解决方案2】:

    像这样:

    sum(A([166, 256, 346, 436, 526]))
    

    例如

    A = [5,4,3,2,1];
    
    A([3, 5]) %// i.e. get the 3rd and 5th column
    

    返回 3 1

    所以sum(A([3, 5])) 返回4

    【讨论】:

    • 是的!但是,首先如何访问列
    • @user2762192 是的,抱歉,我将您的值与您的索引混淆了。这是正确的。
    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多