Logical Indexing instead of for (if) loop
7 views (last 30 days)
Show older comments
Giovanni Gardan
on 20 May 2020
Commented: Giovanni Gardan
on 20 May 2020
I'd like to write the following piece of code usiging Logical indexing instead of for loop with if condition.
As you can see, I commented the original code, wheareas the alternative code is not. CAR_GEN is the main input matrix (with 10000 rows), Y_car_gen is a matrix (10000x10000), En is a column vector (10000x1). So:
- If I run the first code (with for loop) no problem.
- But if I run the second code Matlab says to me that I have a problem:
Error using ^ in N(a-1,1) = -(abs(En(Ngen)))^2*conj(Y_car_gen(Ngen,Ngen));
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar.
To perform elementwise matrix powers, use '.^'.
My question is: why this dimension problem doesn't exist in the first code? Have I to modify something in the second code?
%Initializing N & Ncorr
N = zeros(10000 - 1,1);
Ncorr = zeros(10000 - 1,1);
% for Riga = 2:10000
%
% if CAR_GEN(Riga,1) == abs('G')
% Ngen = CAR_GEN(Riga,2);
% N(Riga-1,1) = -(abs(En(Ngen)))^2*conj(Y_car_gen(Ngen,Ngen));
% Ncorr(Riga-1,1) = En(Ngen).*conj(Ix(Ngen-1));
% else
% Ncar = CAR_GEN(Riga,2);
% N(Riga-1,1) = (abs(En(Ncar)))^2*conj(Y_car_gen(Ncar,Ncar));
% Ncorr(Riga-1,1) = -En(Ncar).*conj(Il(Ncar-N_nodi_gen));
% end
%
% end
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
CAR_GEN2 = CAR_GEN(2:end,:);
a = (CAR_GEN2(:,1) == abs('G'));
Ngen = CAR_GEN2(a,2);
N(a-1,1) = -(abs(En(Ngen)))^2*conj(Y_car_gen(Ngen,Ngen)); %PROBLEM!
Ncorr(a-1,1) = En(Ngen).*conj(Ix(Ngen-1));
b = (CAR_GEN2(:,1) == abs('C'));
Ncar = CAR_GEN2(b,2);
N(b-1,1) = (abs(En(Ncar)))^2*conj(Y_car_gen(Ncar,Ncar)); %PROBLEM!
Ncorr(b-1,1) = -En(Ncar).*conj(Il(Ncar - N_nodi_gen));
0 Comments
Accepted Answer
Walter Roberson
on 20 May 2020
In the original code, you are working with one element at a time, which is a scalar, which is a 1 x 1 matrix, which is square. When you use A^2 then that means the same as A*A which is algebraic matrix multiplication between A and itself, which is defined for square matrices, and for the special case of scalars is the same as squaring the (one) element of the array.
In the newer code you are working with vectors of values. Suppose there were 17 matches, then it might give you a 17 x 1 vector. When you have a 17 x 1 vector algebrically matrix multiplied by itself (because that is what ^2 does) then that would be (17x1) * (17x1) which would fail because the second dimension of the first array (1) does not match the first dimension of the second array (17).
You need to switch to using .^
More Answers (0)
See Also
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!