ArrayBsearch works only with sorted arrays!!! Read Help https://www.mql5.com/en/docs/array/arraybsearch
void OnStart() { double matrix[10]; ArrayInitialize(matrix,0); double matrix_sort[10]; ArrayInitialize(matrix_sort,0); double matrix_neu[10]; ArrayInitialize(matrix_neu,0); int index[10]; ArrayInitialize(index,0); //--- Open log file int fileHandle=FileOpen("Array_Operation",FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_UNICODE); FileWrite(fileHandle,"--- Start"); for(int i=0; i<10; i++) { matrix[i]=i+rand(); FileWrite(fileHandle,"matrix: ",matrix[i]); } FileWrite(fileHandle,"---"); ArrayCopy(matrix_sort,matrix,0,0,WHOLE_ARRAY); ArraySort(matrix_sort); for(int i=0; i<10; i++) { FileWrite(fileHandle,"matrix_sort: ",matrix_sort[i]); } FileWrite(fileHandle,"---"); for(int i=0; i<10; i++) { index[i]=ArrayBsearch(matrix_sort,matrix_sort[i]); FileWrite(fileHandle,"index : ",index[i]); } //--- close log file FileClose(fileHandle); } //+------------------------------------------------------------------+
Result:
index : 0
index : 1
index : 2
index : 3
index : 4
index : 5
index : 6
index : 7
index : 8
index : 9
- www.mql5.com
ArrayBsearch works only with sorted arrays!!! Read Help https://www.mql5.com/en/docs/array/arraybsearch
Result:
thank you for your comment,
you are right, the array is to be sorted first.
But it is working only with the same array:
index[i]=ArrayBsearch(matrix_sort,matrix_sort[i]);
My problem is to find the unsorted indices after sorting the array:
index[i]=ArrayBsearch(matrix, matrix_sort[i]);
This is gives me the same strange results...
I need the indices from the original array for every element - after sorting !
Is there any possibility ?
application example:
I have an array with prices:
index: 0 1 2 3 4
price: 3 9 5 7 1
Now I want to get the indices of the top 3 high price for further calculation.
My sugestion was to sort the price array and compare it to the original array with "ArrayBsearch()" to get the indices.
index: 4 0 2 3 1
price: 1 3 5 7 9
Now I can simply use the last 3 indices for further calculation...
.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
Any Idea how to implement in MQL5?
I know in ArrayObj.mqh are different "Search" methods which give me the position of the found element but I'm new in OOP....
... how can I fill the "matrix" ???
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
CArrayObj *matrix = new CArrayObj;
CArrayObj *matrix_sort = new CArrayObj;
for (int i=0; i<10; i++)
{
matrix.Insert(CObject *element,int pos);
}
}
... for(int i=0; i<10; i++) { index[i]=ArrayBsearch2(matrix,matrix_sort[i]); FileWrite(fileHandle,"index : ",index[i]); } ... //+------------------------------------------------------------------+ //| return index array or -1 | //+------------------------------------------------------------------+ int ArrayBsearch2(const double &array[],double value) { int len=ArrayRange(array,0); for(int i=0; i<len; i++) { if(array[i]==value)return(i); } return(-1); } //+------------------------------------------------------------------+
thx,
realy very helpful!
This is exactly what I need.
Thank you very much.
PS: if any other user has similar problem I propose this too: https://www.mql5.com/en/articles/351
there is the applied case of the problem with the OOP solution ...
- 2011.12.07
- Dmitry Fedoseev
- www.mql5.com
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I want to search some values in a array by using ArrayBsearch and get the position of the matching values as indices.
Here is my example with two identical arrays.
In my opinion I should get a row of "index" from 0 to 9 because the elements of "matrix" and "matrix_sort" are identical.
But I don't get the expected result...
Please simply copy and past into a script and execute.
Than you will find the result in ...\MQL5\Files in a file "Array_Operation"
Here my example:
//+------------------------------------------------------------------+
void OnStart()
{
double matrix[10]; ArrayInitialize(matrix,0);
double matrix_sort[10]; ArrayInitialize(matrix_sort,0);
double matrix_neu[10]; ArrayInitialize(matrix_neu,0);
int index[10]; ArrayInitialize(index,0);
//--- Open log file
int fileHandle=FileOpen("Array_Operation",FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_UNICODE);
FileWrite(fileHandle,"--- Start");
for (int i=0; i<10; i++)
{
matrix[i]=i+rand();
FileWrite(fileHandle,"matrix: ",matrix[i]);
}
FileWrite(fileHandle,"---");
ArrayCopy(matrix_sort, matrix, 0, 0, WHOLE_ARRAY);
for (int i=0; i<10; i++)
{
FileWrite(fileHandle,"matrix_sort: ",matrix_sort[i]);
}
FileWrite(fileHandle,"---");
for (int i=0; i<10; i++)
{
index[i] = ArrayBsearch(matrix, matrix_sort[i]);
FileWrite(fileHandle,"index : ",index[i]);
}
// close log file
FileClose(fileHandle);
}
//+------------------------------------------------------------------+
The arrays "matrix" and "matrix_sort" are identical. (for testing I don't sort the values in matrix_sort)
So there indices are identical too.
Now If I use " ArrayBsearch(matrix, matrix_sort[i]); " to get the indices from "matrix".
But I don't get the row of indices "0 1 2 3 4 5 6 7 8 9", I only get something else....
Please can anybody copy into a script and execute ?
In ...\MQL5\Files\ you find a file named "Array_Operation" with the result.
Where is my mistake ???
PS: is there any library which make the handling of arrays more easy ?
(I only know the Lib posted by bobsley)