Matlab в OpenCV конвертация — оптимизация?

Я конвертирую код Matlab в код OpenCV «C / cpp». И у меня есть некоторые сомнения, как показано ниже.

A = [ 2; 10;  7;  1;  3;  6; 10; 10;  2; 10];
ind = [10; 5; 9; 2];

B является подматрицей A; Элементы матрицы B являются элементами A в местах, указанных в ind,

B = [10 3; 2; 10];

В Matlab я просто использую

B = A(ind);

В C с использованием OpenCV,

for ( int i = 0; i < ind.rows; i++) {
B.at<int>(i,0) = A.at<int>(ind.at<int>(i,0), 0);
}

Есть ли способ сделать это без использования for цикл?

2

Решение

Если вы ищете аккуратный способ копирования. Я бы предложил вам определить функцию
Вот STL-подобная реализация копии в данной позиции

#include<vector>
#include<cstdio>

using namespace std;
/**
* Copies the elements all elements of B to A at given positions.
* indexing array ranges [ind_first, ind_lat)
* e.g
*   int A[]= {0, 2, 10,  7,  1,  3,  6, 10, 10,  2, 10};
*   int B[] = {-1, -1, -1, -1};
*   int ind[] = {10, 5, 9, 2};
*   copyat(B, A, &ind[0], &ind[3]);
*   // results: A:[0,2,10,7,1,-1,6,10,10,-1,-1]
*/
template<class InputIterator, class OutputIterator, class IndexIterator>
OutputIterator copyat (InputIterator src_first,
OutputIterator dest_first,
IndexIterator ind_first,
IndexIterator ind_last)
{
while(ind_first!=ind_last) {
*(dest_first + *ind_first) = *(src_first);
++ind_first;
}
return (dest_first+*ind_first);
}int main()
{
int A[]= {0, 2, 10,  7,  1,  3,  6, 10, 10,  2, 10};
int B[] = {-1, -1, -1, -1};
int ind[] = {10, 5, 9, 2};

copyat(B, A, &ind[0], &ind[3]);

printf("A:[");
for(int i=0; i<10; i++)
printf("%d,", A[i]);
printf("%d]\n",A[10]);
}
0

Другие решения

Других решений пока нет …