简体   繁体   中英

Matlab find minimum value by ignoring zero and certain condition

so i have this data

 yi =

    -1
     1
     1
    -2
     4
data=
    1.0000         0    1.0000    0.2000    1.0000    1.0000
         0         0         0    0.5000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
         0         0         0         0    1.0000         0
    1.0000         0         0         0    1.0000         0

and i short that data become like this

sdata =

     0    0.2000    1.0000    1.0000    1.0000    1.0000
     0         0         0    0.5000    1.0000    1.0000
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
     0         0         0         0         0    1.0000
     0         0         0         0    1.0000    1.0000

with this rule

  1. if yi >= 0 then output = max row sdata
  2. if yi < 0 theris 2 conditions:

    a. if sdata member only 0 and 1 then output = min row sdata =0

    b. if sdata member including number between 0 and 1 then output = min sdata except zero

so i want to find an output like this

output=

  0.2000
  1.0000
  1.0000
  1.0000
       0
  1.0000

i still stuck to this point

 for i=1:5
        if yi(i)>=0
            output(i)=max(sdata(i,:));
        else
            output(i)=min(sdata(i,:));    
        end;
    end;
    outputnya=output'

Try this:

else
  minVal = min(sdata(i,sdata(i,:)>0))
  if minVal == 1
    output(i)=min(sdata(i,:));
  else
    output(i) = minVal;
  end;
end;

Explanation: sdata(i,:)>0 returns table of 0 and 1, sdata(i,sdata(i,:)>0) returns element which meet specified condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM