Vectorization and a strange result

Hi,
I am struggling since few hours with a vectorized operation.
I have some vector quantities, and I wanted to vectorize. I put in the attachment a .mat file.
For testing, I compute the fx1 and fx_1 in this way
fx1 = const_1 .* exp(-1j.*beta(1:2)'.*const_2)./const_2 .* t;
fx_1 = const_1 .* exp(-1j.*beta(1)*const_2)./const_2 .* t;
where beta is a vector. I would expect that the first row of fx1 and fx_1 coincide, but they don't. I am very confused what I am missing here. Ideally, I would like to do this
fx1 = const_1 .* exp(-1j.*beta'.*const_2)./const_2 .* t;
instead of multiplying in a for loop for each beta(i).

Answers (1)

Your ‘mytest.mat’ file contains:
fx_1: [1×150 double]
beta: [1×25 double]
t: [1×150 double]
const_1: [1×150 double]
const_2: [1×150 double]
fx1: [2×150 double]
It would help to know what you want to do with those vectors and the ‘fx1’ matrix. That is not obvious from the code you posted.

4 Comments

Sure.
I want to do this
fx1 = const_1 .* exp(-1j.*beta'.*const_2)./const_2 .* t;
instead of this
for i = 1 : LENGTH_beta
fx(i,:) = const_1 .* exp(-1j.*beta(i)*const_2)./const_2 .* t;
end
I thought it would work but I guess I am missing something.
This works:
D = load('mytest.mat')
beta = D.beta;
t = D.t;
const_1 = D.const_1;
const_2 = D.const_2;
fx1 = const_1 .* exp(-1j.*beta'.*const_2)./const_2 .* t;
figure
hp1 = plot(t, real(fx1), '-b');
hold on
hp2 =plot(t, imag(fx1), '-r');
hp3 = plot(t, abs(fx1), '--k');
hold off
grid
legend([hp1(1),hp2(1),hp3(1)], '\Re{fx1}', '\Im{fx1}', '|fx1|', 'Location','NW')
figure
mesh(abs(fx1))
set(gca, 'ZScale','log')
xlabel('t')
ylabel('\beta (index)') % % 'beta' Is Complex, So Plotting The Index Valuesbeta’ Is Complex, So Plotting The Index Values
zlabel('|fx1|')
The 2D plot is not very informative, however the mesh plot may give a bit more insight.
I see that you are plotting fx1. But, if I am doing it right, the fx1 is still different than the fx computed in the for loop. In your result, do you see them equal? I double check with isequal
D = load('mytest.mat');
beta = D.beta;
t = D.t;
const_1 = D.const_1;
const_2 = D.const_2;
fx1 = const_1 .* exp(-1j.*beta'.*const_2)./const_2 .* t;
fx = zeros(length(beta),150);
for i = 1 : length(beta)
fx(i,:) = const_1 .* exp(-1j.*beta(i).*const_2)./const_2 .* t;
end
isequal(fx,fx1)
You are computing ‘fx’ differently in the looop than in the vectorised calculation.
If you plot them:
for i = 1 : length(beta)
fx(i,:) = const_1 .* exp(-1j.*beta(i).*const_2)./const_2 .* t;
end
figure
mesh(abs(fx))
set(gca, 'ZScale','log')
xlabel('t')
ylabel('\beta (index)') % ‘beta’ Is Complex, So Plotting The Index Values
zlabel('|fx1|')
you will see that they are essentially just mirror-images of each other with respect to the z-axis. The magnitudes are of courrse different, the vectorised approach goes from to , and the loop goes from to . Chioose whatever version makes sense in the context of what you want to do. I have no idea what that is.
I would use the fully-vectorised approach (in my code), since to me that makes more sense.

This question is closed.

Products

Release

R2020a

Asked:

on 14 Oct 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!