Array Indexing in a for loop

조회 수: 3 (최근 30일)
Karthik Nagaraj
Karthik Nagaraj 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
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
Stephen23
Stephen23 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
"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 2020년 2월 5일
Thank you

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2020년 2월 5일
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.
  댓글 수: 1
Karthik Nagaraj
Karthik Nagaraj 2020년 2월 5일
Thank you for the swift reply

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by