what can i do for this problem?

1 view (last 30 days)
Tia
Tia on 27 Jul 2013
i want to use circshift on each 4096 blocks. each block has 8x8. what can i do?
example :
val(:,:,1) =
1.0e+003 *
1.5601 -0.0420 0.0307 -0.0164 -0.0116 -0.0204 -0.0001 -0.0001
-0.0117 0.0063 -0.0138 -0.0298 -0.0134 -0.0001 0.0009 0.0003
-0.0000 -0.0066 -0.0154 0.0001 -0.0002 -0.0007 0.0000 0.0001
0.0139 0.0092 0.0001 -0.0004 0.0006 0.0000 0.0001 -0.0002
0.0001 0.0003 0.0004 -0.0003 -0.0001 0.0005 -0.0000 0.0003
-0.0002 0.0001 0.0001 -0.0003 -0.0003 0.0003 0.0000 0.0001
-0.0004 -0.0003 -0.0002 -0.0002 -0.0001 -0.0004 -0.0001 0.0001
0.0001 -0.0004 -0.0000 0.0002 -0.0000 -0.0002 0.0002 0.0003
until
val(:,:,4096) =
888.0000 -17.6230 -5.2495 -0.4683 0 0.0909 -0.2610 -0.2792
18.2375 12.0902 -14.1269 -10.2305 -13.2117 -0.1457 -0.2282 0.0176
-6.8663 6.7403 8.3676 11.7774 -0.3663 0.1670 -0.3018 -0.3649
20.8197 -0.1267 -10.9207 0.2049 -0.1160 -0.0591 0.1562 -0.2024
0.2500 -0.2738 0 -0.2663 -0.2500 0.8486 0 0.0949
12.3305 -0.2777 -0.7394 -0.1323 -0.1247 -0.0385 0.4240 -0.1999
0.4087 0.2580 -0.0518 0.5184 0.4223 0.2034 -0.1176 0.2729
0.1247 0.4444 0.1992 -0.2810 0.2159 0.2496 -0.2789 -0.2565
thanks

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 27 Jul 2013
val=rand(8,8,4096); % Example
m=size(val,3);
a=arrayfun(@(x) circshift(val(:,:,x),-1),1:m,'un',0);
out=reshape(cell2mat(a),8,8,[]);
  5 Comments
Jan
Jan on 27 Jul 2013
@Azzi: Again I suggest to avoid anonymous functions, here in arrayfun. See some timings in my answer.
Jan
Jan on 27 Jul 2013
@Azzi: No idea why the first comment was there. I have some timing problems in the forum currently. Sometimes after I hit the Save butting, its title is saved to "Saving..." and nothing happens for a minute. Then it is set back to "Save" without any changes.
I've deleted the misplaced first comment.

Sign in to comment.

More Answers (1)

Jan
Jan on 27 Jul 2013
Edited: Jan on 27 Jul 2013
There is no need to encapsulate circshift in arrayfun:
val = rand(8,8,4096);
tic;
m = size(val,3);
a = arrayfun(@(x) circshift(val(:,:,x),-1),1:m,'un',0);
out = reshape(cell2mat(a),8,8,[]);
toc
% Elapsed time is 0.411869 seconds.
A simple loop is faster than ARRAYFUN:
tic;
out2 = zeros(size(val));
for k = 1:m
out2(:,:,k) = circshift(val(:,:,k), -1);
end
toc
% Elapsed time is 0.379831 seconds.
But circshift operate on 3D arrays directly also:
tic;
out3 = circshift(val, -1);
toc
% Elapsed time is 0.006587 seconds.
isequal(out, out2, out3)
% TRUE
This is 62 times faster (R2009a/64/Win7) and looks much easier.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!