I need to know how matlab do A(B) where A and B are 2 vectors
古いコメントを表示
let
A=[1 2 4 6 9]
B=[2 1 3 5]
C=A(B)
so
C=[2 1 4 9]
How is matlab do this very fast is used the following code
for i=1 to 4
c(i)=A(B(i)
end
I think no since it do this very fast even if the vector size is 100000 so how is matlab do this
I need the answer since I need to program it in C to become fast as in matlab please any help
2 件のコメント
Matt J
2012 年 11 月 17 日
How MATLAB accelerates its code is not public domain knowledge, but it is likely that multithreading is involved.
@Matt J: C = A(B) is not and should not be multi-threaded, because B is not necessarily unique. Therefore there is no strategy to distribute the job to multiple workers.
An important difference between this Matlab code and a naive C-implementation is the range check. The C-code is much slower, if the limits are checked in each iteration, because the required IF branching prevents a successful pipelining of the code inside the processor.
Unfortunately a range check seems to happen for logical indexing also. A C-code implementation takes less than the half time, if it performs only 1 check of the length of the index array.
回答 (1 件)
Matt Fig
2012 年 11 月 17 日
0 投票
I believe that these basic things are done just like you show, but in compiled C-code. Thus it is much faster that using interpreted MATLAB code, even with the JIT.
10 件のコメント
Mousa
2012 年 11 月 17 日
Matt Fig
2012 年 11 月 17 日
Code, written in the C language, that has been compiled. If you don't know what C is, look on wikipedia.
Mousa
2012 年 11 月 17 日
Matt Fig
2012 年 11 月 17 日
How are you making your comparison, exactly?
Mousa
2012 年 11 月 17 日
Mousa, you are not going to make headway comparing pure MATLAB to pure C. What I am talking about is more like this. Here is a very simple mex function that does the indexing.
#include "mex.h"
// saved as index_mex.c
// For row vectors A and B, this does A(B)
// NO ERROR CHECKING... MAY CRASH MATLAB IF SIZES DIFFER
// OR IF ANY(B>NUMEL(A)) OR IF B HAS NON INTEGERS...
void yindexx(double *x, double *y, double *z, size_t n)
{
mwSize i;
for (i=0; i<n; i++)
{
*(z+i) = *(x+((int)*(y+i))-1);
}
}
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *y,*z,*x;
size_t mrows,ncols;
if(nrhs!=2)
mexErrMsgIdAndTxt( "MATLAB:index_mex:invalidNumInputs",
"Two inputs required.");
/* get the input x */
x = mxGetPr(prhs[0]);
/* create a pointer to the vector matrix y */
y = mxGetPr(prhs[1]);
/* get the dimensions of the vector input y */
ncols = mxGetN(prhs[1]);
/* set the output pointer to the output matrix */
plhs[0] = mxCreateDoubleMatrix( 1, (mwSize)ncols, mxREAL);
/* create a C pointer to a copy of the output matrix */
z = mxGetPr(plhs[0]);
yindexx(x,y,z,ncols);
}
Now once this is compiled, I test it by writing a little script:
N = 1e6;
A = randi(N,1,N);
B = randi(N,1,N);
tic
for ii = length(B):-1:1
C(ii) = A(B(ii));
end
toc
tic
C2 = A(B);
toc
tic
C3 = index_mex(A,B);
toc
isequal(C,C2,C3)
Now look at the output:
>> test_index_mex
Elapsed time is 0.239547 seconds.
Elapsed time is 0.056887 seconds.
Elapsed time is 0.026302 seconds.
ans =
1
So the compiled C-mex is MUCH faster than the MATLAB FOR loop and even faster than the built-in indexing. It is faster than the built-in indexing because I have included no error checking beyond the number of inputs. Hopefully you begin to see what I am talking about!
Mousa
2012 年 11 月 18 日
Mousa
2012 年 11 月 18 日
Matt Fig
2012 年 11 月 18 日
The mex function I posted above needs to be compiled by MATLAB to run. If you have never used MEX, you need to do this:
mex -setup
This will guide you through the process of finding a compiler on your system. I have used the Microsoft Software Development Kit (SDK) 7.1 for the above tests. Then make sure that the mex-file is on the MATLAB path to compile it. You also might want to do:
help mex
Mousa
2012 年 11 月 18 日
カテゴリ
ヘルプ センター および File Exchange で MATLAB Compiler についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!