【问题标题】:finding the first value of one matrix after satisfying condition of another matrix在满足另一个矩阵的条件后找到一个矩阵的第一个值
【发布时间】:2014-06-03 22:16:50
【问题描述】:

请帮我解决这个问题...

a = [1 2 3 4 5 6 7 8 9 10]  
b = [12 4 13 7 5 7 8 10 3 12]  
c = [4 5 3 2 6 7 5 3 4 5]

我必须找到a 上的第一个值,如果b 上的值连续3 个以上的位置小于10 并且开始满足条件的索引。在找到相同索引的b 的值后,还有c 的值。

Ans 应该是 b=4 的索引,a=4value for a =4c=2 的索引

提前谢谢你

【问题讨论】:

    标签: arrays matlab matrix conditional-statements


    【解决方案1】:

    您可以使用strfind 作为一种方法 -

    str1 = num2str(b <10,'%1d') %%// String of binary numbers
    indx = strfind(['0' str1],'0111') %%// Indices where the condition is met
    ind = indx(1) %%// Choose the first occurance
    a_out = a(ind) %%// Index into a
    c_out = c(ind) %%// Index into c
    

    输出 -

    ind =
         4
    
    a_out =
         4
    
    c_out =
         2
    

    【讨论】:

    • 如果 b 以小于 10 的 3 个值开头,我认为这不会起作用。
    • @carlosdc 找到那个很好,已修复!
    • 不错,已修复+1。
    • 但是我在 excel 中有大量数据,很难找到条件匹配的索引...请给出一些可能适用于所有情况的通用代码....ab 的值和 c 只是举例......谢谢你们的帮助
    • @user3551734 这是一个通用代码。你有没有用你的大数据试过这个?
    【解决方案2】:

    要找到低于阈值的给定数量个连续值,您可以将conv应用于0-1值的向量比较:

    threshold = 10; %// values "should" be smaller than this
    number = 4; %// at least 4 consecutive occurrences
    
    ind = find(conv(double(b<threshold), ones(1,number), 'valid')==number, 1);
    %// double(b<threshold) gives 0-1.
    %// conv(...)==... gives 1 when the sought number of consecutive 1's is reached
    %// find(... ,1)  gives the first index where that happens
    a_out = a(ind);
    c_out = c(ind);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-21
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多