i have a constant lets say c=-6; and a vector n=-20:.5:20; and i have to find (c)^n ? how can i do this?
MATLAB: Some constant to the power vector
MATLABvecors
Related Solutions
>> A = complex(randi([-3,3],4,4),randi([-3,3],4,4)) % fake data
A = -2 + 3i -2 + 1i 2 - 3i -2 + 1i -2 + 0i 3 + 0i -3 + 3i 0 + 0i 2 + 0i 2 - 2i 3 + 3i 0 - 3i 1 - 2i -3 - 2i 3 + 2i 0 - 2i>> B = complex(real(A).*(real(A)<=0.5),imag(A)) % set real(A)>0.5 to 0.
B = -2 + 3i -2 + 1i 0 - 3i -2 + 1i -2 + 0i 0 + 0i -3 + 3i 0 + 0i 0 + 0i 0 - 2i 0 + 3i 0 - 3i 0 - 2i -3 - 2i 0 + 2i 0 - 2i
If you have a modern Matlab version >= 2016b, use the auto expanding instead of an explicit repmat:
% Ipc = repmat(I(:,jt2),[1,npmax(jz)-1]);
Ipc = I(:,jt2);... CnE(:,1:npmax(jz)-1,jl)) .* Ipc * dt / hb
I guess, that dt and hb are scalars (please do not let the readers guess!). Then remember, that Matlab evaluate the code from left to right:
Y = X * dt / hb
is equivalent to:
Temp = X * dt;Y = Temp / hb;
These are 2 matrix operations. But you can combine the scalar operations:
Y = X * (dt / hb)
This is 1 matrix operation and a cheap scalar division.
As Matt J has explained already, operating on subarrays as in |dCnE(:,2:npmax(jz)) is expensive. Better crop the data, such that you can omit the ugly indexing.
I cannot run it by my own, because you did not provide the input values, but compare it optically:
Ipc=repmat(I(:,jt2),[1,npmax(jz)-1]);dCnE(:,2:npmax(jz))=(sigma0*CnZ(:,2:npmax(jz),jl)-sigma*CnE(:,2:npmax(jz),jl)+ ... vs2*sigma*CnE(:,1:npmax(jz)-1,jl)).*Ipc*dt/hb+CnB(:,2:npmax(jz),jl)*(1-exp(-dt/tau2))... +vs2*(beta0*CnZ(:,1:npmax(jz)-1,jl).*(Ipc.^2))*dt/(2*hb)-CnE(:,2:npmax(jz),jl)/tau1*dt;
or:
c1 = 1 - exp(-dt / tau2); % Calculate constants outside the loops!
Ipc = I(:,jt2);dCnE = (sigma0 * CnZ(:, :, jl) - sigma * CnE(:, :, jl) + ... vs2 * sigma * CnE(:, :, jl)) .* (Ipc * (dt / hb)) + ... CnB(:,:,jl) * c1 + ... (vs2 * beta0 * dt / (2*hb)) * CnZ(:, :, jl) .* (Ipc .^ 2) - ... CnE(:, :, jl) / (tau1 * dt);
This can be simplified a little bit for the CnE term:
dCnE = (sigma0 * CnZ(:, :, jl) + ... CnE(:, :, jl)) .* (Ipc * ((vs2 * sigma - sigma) * dt / hb)) + ... CnB(:,:,jl) * c1 + ... (vs2 * beta0 * dt / (2*hb)) * CnZ(:, :, jl) .* (Ipc .^ 2) - ... CnE(:, :, jl) / (tau1 * dt);
Methods:
- Combine the scalar operations to reduce the number of matrix operations.
- Move constants out of the loop.
- Crop the data outside the loops instead of applying an a:b indexing in each iteration.
- Use auto-expanding instead of inflating by repmat. bsxfun is sometimes faster or slower than auto-expanding - try it.
- Avoid repeated calculations. In your case e.g. Ipc(:,1)*dt/hb is computed several times. Using this avoids this:
C1 = I(:, jt2) * (dt/hb);
The latter includes another idea: You inflate I(:,jt2) at first by Ipc = repmat(...), and waste time with cropping out a subvector Ipc(:,1) later on. Better create the subvector once only: Ipc = I(:,jt2).
Finally: Replace
non0=Cn0(:,jl); non0(non0<0)=0; Cn0(:,jl)=non0;
by:
Cn0 = max(Cn0, 0);
and move it outside the loops, because you do not use the cleaned values inside the loop.
Best Answer