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 Kommentare

How MATLAB accelerates its code is not public domain knowledge, but it is likely that multithreading is involved.
Jan
Jan am 18 Nov. 2012
Bearbeitet: Jan am 18 Nov. 2012
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt Fig
Matt Fig am 17 Nov. 2012

0 Stimmen

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 Kommentare

please, what do you mean by compiled C-code
Code, written in the C language, that has been compiled. If you don't know what C is, look on wikipedia.
yes, I know C language, but when I write this code in C language it take time 100 times more than time consumed in matlab, so I think Matlab will implement this function in a different way,ie not using sequential loop
How are you making your comparison, exactly?
used tic toc in matlab and using start and end clock timer in c for matrix of 1X1000000 value in matlab I write A(B) inside loop of 1 iteration and calculate the time and in c I made this using loop and also calculate the time
Matt Fig
Matt Fig am 18 Nov. 2012
Bearbeitet: Matt Fig am 18 Nov. 2012
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!
Thanks for your answers, but what is mex.h, in which library can I find it to run the program using codeblocks or VS
I think is inside matlab not inside library for c Language
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
Thanks

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 17 Nov. 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by