0

I want to intersect some fractional values:

frac_value = intersect (find(xmin > 0) , find(xmin < 1))

where xmin is a large vector of fractional values. I have printed xmin values till 0.16f, where some of them are showing values 0.0000000000000000, but frac_value returning it's position. I have no idea why. How can I perfectly get the positions of fractional values only?

Any help regarding this will be appreciated.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Cherry
  • 971
  • 1
  • 10
  • 20

1 Answers1

2

The code you wrote is a bit obfuscated. One way to make this more clear is to do:

frac_value = find(xmin > 0 & xmin < 1);

What you are essentially doing is finding the indices of xmin that are both greater than 0 and less than 1. Instead of using intersect, just use find.


If you want to find the positions of where these values lie, then the above code is perfectly acceptable. However, I suspect that this is not what you're intending due to the title of your post. If you want to find the actual fractional values, you need to index into xmin with frac_value instead:

values = xmin(frac_value);

However, I wouldn't use find or intersect here at all. What is more efficient and simpler is to use logical indexing without find or intersect (which I will argue is faster performance-wise):

values = xmin(xmin > 0 & xmin < 1);

values should now contain the values of xmin that are between 0 and 1, rather than the locations of them.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • I have another question, what should i use in matlab when I want to use logical and (& or &&) , logical or (| or ||)? – Cherry May 09 '15 at 20:45
  • 1
    there're some info for `&, &&, |, ||` here: https://truongnghiem.wordpress.com/2010/10/21/short-circuit-logical-operators-matlab/ – scmg May 09 '15 at 20:52
  • @Cherry - Check here as well: http://stackoverflow.com/questions/1379415/whats-the-difference-between-and-in-matlab – rayryeng May 09 '15 at 20:53
  • Sorry, it is still showing the same problem. like 'frac_value' are showing those position which are actually '0.0000000000000000' , do not know what to do – Cherry May 09 '15 at 20:55
  • rayryeng: sure I can. But probably I do not know how to accept an answer :P :P – Cherry May 09 '15 at 21:02
  • click on the approved icon under the vote number to accept an answer :p – scmg May 09 '15 at 21:08
  • @Cherry - Try changing the format: `format long g`. `format long g` only shows floating point decimal places if merited. It automatically figures out the best display type for you given the number. – rayryeng May 09 '15 at 22:16