| nonolt (3) | |
|
Hello, I am new in c++ and I am having difficulties with finding values in two vectors. Basically, I have two constant integer vectors u and v . They have the same length. I would like to select all the values in u and v, that respect those statements: u>= x1 & u<= x2 & v>= x3 & v<= x4 x1, x2, x3, x4 are predefined integers. If it can help, below is the code i use to do that in Matlab: idx = find(u>=x1 & u<=x2 & v>=x3 & v<=x4); A=u(idx); B=v(idx); But I have no idea how to translate that in c++. Any suggestions? | |
|
Last edited on
|
|
| Smac89 (195) | |
| Can you explain what your matlab program is doing? Because coming from a c++ perspective, it looks like it is either performing a bitwise and on the values or accessing the address of the values. | |
|
|
|
| cire (2347) | |||
Something like this, maybe?
| |||
|
Last edited on
|
|||
| nonolt (3) | |||
|
Thanks for quick response. Actually, I kind of changed my mind ;-). I want to select values from a third vector based on the indices from the first two vectors. I almost managed to do it. However something is wrong at line 35-36. When I use line 35 it works. When I use line 36 it doesn't (I mean it does not select any data although there are plenty within the statement). Any idea why? Is it something related to the negative / positive sign? Also, I know it's bad coding (I started C++ 2 days ago), so if you have a couple of simple neat advice to improve the code, you're welcome!
| |||
|
Last edited on
|
|||
| Smac89 (195) | |
| Only thing I will recommend is for your for-loop to have as a condition that the end of the file has not been reached. | |
|
|
|
| cire (2347) | ||||
Since the code I posted returns a container of indices into the first and second vector that could be used to index the third vector, that would seem a rather trivial adjustment. As for your problem, I suspect it has more to do with the loop beginning on line 44 than the values given on lines 35/36. The inboxdens array should be populated before the loop. Based on my previous code, but untested:
| ||||
|
|
||||
| nonolt (3) | |
| Thanks cire. It took me a bit of time to understand every lines of your code but I have learned a lot through it. It looks like I need to learn/know the c++ keywords to be able to adopt a generic programming style. | |
|
|
|