【问题标题】:Is there a way to sum the extracted positions from a matrix using sum() function in matlab有没有办法使用matlab中的sum()函数对矩阵中提取的位置求和
【发布时间】:2021-10-10 19:26:02
【问题描述】:
Matrix =[1 2 3 4 5 6; 6 5 4 3 2 1];

[f0,c0]=find(M<4); 
[f1,c1]=find(M>4); 

您好,我使用 find function() 提取了给定这些条件的位置,但我似乎没有找到一种方法来使用 Matlab 中的 sum() 函数将这些相应的位置相加!?

【问题讨论】:

  • 给定矩阵的输出是什么?你是如何使用 sum 函数的?
  • Hi [f0,c0] 中的值总和的输出应该是 12(1,2,3 位于文件 1 + 321 位于文件 2),与其他条件相同(5 +6 位于文件 1 +6+5 位于文件 2)
  • Result=sum(Matrix(f0,c0)) 但结果不正确
  • %3) 使用下一个矩阵 M=[1 2 3 4 5 6; 6 5 4 3 2 1]; s=尺寸(米); largo_fila=size(M,1); largo_columna=大小(M,2); s1=0; s2=0;对于 i=1:1:largo_fila 对于 a=1:1:largo_columna 如果 M(i,a)=4 s2=s2+M(i,a);结束结束结束
  • edit您的帖子包含详细信息

标签: matlab matrix sum


【解决方案1】:

就像 Cris Luengo 写的那样,只需使用:

M=[1 2 3 4 5 6; 6 5 4 3 2 1];
sum(M(M<4)) % ans = 12
sum(M(M>4)) % ans = 22

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2014-08-21
    • 2013-06-08
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多