Reputation: 189
I have a Matlab code that goes like this
s=[0.5 0.6 0.7];
u=[0.3618 0.9826 0.7237 0.0346 0.5525 0.0692 0.8949 0.1384
0.3418 0.9226 0.7213 0.0346 0.7525 0.0692 0.8949 0.1384
0.3318 0.9326 0.7237 0.0336 0.5575 0.0792 0.8949 0.1385]
A= u(1:2:7); % Here u is a 1-D vector and hence A
B=u(2:2:8); % Here u is a 1-D vector and hence B
C=mod(s(1)-(A+B),1);
I want to implement this code for the other two values of s
also using next 8
values of u
i.e now my code becomes
A=u(9:2:15);
B=u(10:2:16);
C=mod(s(2)-(A+B),1);
Similarly for last value of s. But each time i need the next 8
values of u
. How do i code this so that it takes less time.
Upvotes: 0
Views: 39
Reputation: 778
So you start with a 24 element array in u
that you wish to perform this operation on in a vectorized fashion. I assume you have many more elements but that they all fit in memory. The way to do this is to reshape u
to where you want the elements to be. You can do this via:
u1 = reshape(u1,[2,4,3]);
From there you also need to modify s
to match it
s1 = permute(s,[1 3 2]);
Finally, you can calculate your C
matrix in vectorized form
C1 = mod(s1-sum(u1),1);
For this problem, this gives a 1x4x3
matrix where the 3rd dimension represents each set of 8. From there you can then extract the problem set you want
C = squeeze(C1(1,:,1));
Upvotes: 2