Array Indexing in a for loop

1 visualización (últimos 30 días)
Karthik Nagaraj
Karthik Nagaraj el 5 de Feb. de 2020
Editada: Stephen23 el 5 de Feb. de 2020
I am still a novice in MATLAB. I have some questions regarding the array indexing in for loop. My sample program is as below. It runs as intended
%%classical beamformer
N=15;
steer_step=0.1;
theta=-90:steer_step:90;
for q=1:length(theta)
a(q,:)=exp(-1j*w*sind(theta(q))*(-(N-1)/2:(N-1)/2));.
CBF(1,q)=a1(q,:)*Cxx*a1(q,:)'; % Cxx previously generated Matrix of NxN order
end
So I have some doubts regarding logical application in these lines of the code.
1) a(q,:)=exp(-1j*w*sind(theta(q))*(-(N-1)/2:(N-1)/2));.
Should we use elementwise multiplication .* for sind term since q is an array or the normal multiplication is enough.
For any case when should we use element wise and not element wise operation. I know how it works, but not exactly where to use element wise operation.
2) CBF(1,q)=a1(q,:)*Cxx*a1(q,:)';
When should we use or why should we use CBF(1,q) with indexing and simply 'CBF' without any indexing.
For any case when should we define variable/array in LHS to store the value with or without indexing.
  2 comentarios
Stephen23
Stephen23 el 5 de Feb. de 2020
Editada: Stephen23 el 5 de Feb. de 2020
"For any case when should we use element wise and not element wise operation. I know how it works, but not exactly where to use element wise operation"
Matrix operations are for linear algebra, so if you are not doing linear algebra, then use element-wise operations. In almost all cases when beginners don't know what linear algebra is, or what matrix multiplication is, or why matrices have such strange ways of behaving, they should be using element-wise operations.
"When should we use or why should we use CBF(1,q) with indexing and simply 'CBF' without any indexing."
These are two completely different operations:
  1. Use indexing when you want to redefine some/all elements of an array.
  2. Allocate an array to a variable (what you called "without any indexing") when you want to allocate the RHS to a variable.
Karthik Nagaraj
Karthik Nagaraj el 5 de Feb. de 2020
Thank you

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 5 de Feb. de 2020
Part 1: when one of the quantities you're multiplying is a scalar, the * and .* operators are equivalent.
x = magic(4)
y1 = 2*x
y2 = 2.*x
isequal(y1, y2) % true
In the specific case you identified, theta is a non-scalar array but theta(q) is one value from that array. That means sind(theta(q)) is a scalar.
This documentation page goes into more detail about the difference between array operations (like .*) and the matrix operations (like *.)
Part 2: "CBF(1, q) = ..." assigns whatever's to the right of the equals sign into the one element in row 1, column q of CBF.
"CBF = ..." overwrites the whole of CBF with whatever's to the right of the equals sign.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by