what would be the c++ equivalent to this matlab code? I started it off but i’m not sure what value B would be, i think A is correct.
MATLAB
array = (-1:.001:1)';
A = max(find(array < 1.0e-2));
B = min(find(array > 1 - 1.0e-2));
C++ attempt
for(i = 0; i < array.size; i++){
if(array[i] < 1.0e-2){
k++
A = k;
}
if(array[i] > (1- 1.0e-2)){
//not sure what to do here
B = ?;
}
}
For the second bit, I would do this instead inside the same loop:
and initialize
Btoarray.sizebefore the loop.Essentially, you are finding the element with the lowest index in the array which is also larger than
1 - 1.0e-2. If you start at the element with the highest index and then traverse the entire array and update each time you satisfy the criteria, you will end up with what you need. A similar logic applies for the first part, but you will need to setA = i+1instead of incrementingkbecause there is no guarantee the array is sorted.