Prev: getting error while exporting data to MS Access
Next: Colormap that works great in both colored and grayscale prints
From: Jan Simon on 24 Jul 2010 15:54 Dear Matt, > Result=bsxfun(@eq,B,permute(A(:),[3,2,1])); > then each slice Result(:,:,i) will be the the result of find(B==A(i)); Nice. For a vector, RESHAPE might be a little bit simpler: Result = bsxfun(@eq, B, reshape(A, 1, 1, numel(A))); Jan
From: Matt J on 24 Jul 2010 16:41 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i2fgcr$1oi$1(a)fred.mathworks.com>... > Dear Matt, > > > Result=bsxfun(@eq,B,permute(A(:),[3,2,1])); > > then each slice Result(:,:,i) will be the the result of find(B==A(i)); > > Nice. For a vector, RESHAPE might be a little bit simpler: > Result = bsxfun(@eq, B, reshape(A, 1, 1, numel(A))); ============= Good point, Jan. The short test below shows that reshape() is a fair bit faster. >> A=rand(1e7,1); tic; permute(A,[3,2,1]);toc; tic; reshape(A,1,1,[]);toc Elapsed time is 0.166652 seconds. Elapsed time is 0.004647 seconds.
From: Jan Simon on 24 Jul 2010 17:32 Dear Matt J, > >> A=rand(1e7,1); tic; permute(A,[3,2,1]);toc; tic; reshape(A,1,1,[]);toc > Elapsed time is 0.166652 seconds. > Elapsed time is 0.004647 seconds. Which Matlab? 1.5GHz Pentium-M, Matlab 2009a, 512MB, WinXP-32bit: A = rand(1e7, 1); clear('y'); tic; y = permute(A, [3,2,1]); toc >> 0.35 sec clear('y'); tic; y = reshape(A, 1, 1, []); toc >> 0.000439 sec clear('y'); tic; y = reshape(A, 1, 1, numel(A)); toc >> 0.000019 But if I put the lines in a function (more realistic, and of course repeated 1000 times...), RESHAPE(1, 1, []) is only 30% slower than RESHAPE(1, 1, NUMEL(A)). Reshape is much faster than PERMUTE, because it replies a shared data copy, while PERMUTE duplicates the data. See: "format debug". @guyz Smarty: I hope you are not too much frightend by this discussion. For [1x3] vectors the timing between RESHAPE and PERMUTE is absolutely meaningless! My for loop is simple, Matt's BSXFUN is efficient Kind regards, Jan
From: Matt J on 24 Jul 2010 17:55 "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i2fed5$s68$1(a)fred.mathworks.com>... > > If you do > > Result=bsxfun(@eq,B,permute(A(:),[3,2,1])); > > then each slice Result(:,:,i) will be the the result of find(B==A(i)); Should be "will be the resulf of B==A(i)"
From: guyz Smarty on 24 Jul 2010 20:25 Hi all, Thank you very much to all of you for your quick response and help. @ Nic Roberts I know that the problem I presented here can be easily solved with FOR loops. But I have to do calculations for a matrix containing more than 100000 rows. In that case, loops are not a good choice(I suppose). Therefore, I was looking for a solution without using any FOR loops. @someone The use of Cell array is really a new thing for me. I will keep that in mind and try to use that in future. @Matt Thank you. You gave me the direction that I wanted to follow. But, as you too would agree, bsxfun is not a very easy function to comprehend. So, I want to learn more about this. However, I have managed to get rid of my problem as of now using bsxfun. @Jan No..No.. I am not frightened at all with all those discussions. In fact, I got to learn many things here. The use of cell array, pre-allocation of variables and bsxfun functions are just a few to name for. So the credit goes to all of you. Thanks once again to all.. ==================================================== "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i2fm4k$pov$1(a)fred.mathworks.com>... > Dear Matt J, > > > >> A=rand(1e7,1); tic; permute(A,[3,2,1]);toc; tic; reshape(A,1,1,[]);toc > > Elapsed time is 0.166652 seconds. > > Elapsed time is 0.004647 seconds. > > Which Matlab? > > 1.5GHz Pentium-M, Matlab 2009a, 512MB, WinXP-32bit: > A = rand(1e7, 1); > clear('y'); tic; y = permute(A, [3,2,1]); toc > >> 0.35 sec > clear('y'); tic; y = reshape(A, 1, 1, []); toc > >> 0.000439 sec > clear('y'); tic; y = reshape(A, 1, 1, numel(A)); toc > >> 0.000019 > > But if I put the lines in a function (more realistic, and of course repeated 1000 times...), RESHAPE(1, 1, []) is only 30% slower than RESHAPE(1, 1, NUMEL(A)). > Reshape is much faster than PERMUTE, because it replies a shared data copy, while PERMUTE duplicates the data. See: "format debug". > > @guyz Smarty: I hope you are not too much frightend by this discussion. For [1x3] vectors the timing between RESHAPE and PERMUTE is absolutely meaningless! My for loop is simple, Matt's BSXFUN is efficient > > Kind regards, Jan
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: getting error while exporting data to MS Access Next: Colormap that works great in both colored and grayscale prints |