0
0
MATLABdata~10 mins

Element-wise operations (.*, ./, .^) in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Element-wise operations (.*, ./, .^)
Start with two arrays
Choose element-wise operator
Apply operator to each pair of elements
Create new array with results
End
Element-wise operations apply the chosen operator to each matching element of two arrays, producing a new array of the same size.
Execution Sample
MATLAB
A = [1 2 3];
B = [4 5 6];
C = A .* B;
D = A ./ B;
E = A .^ 2;
This code multiplies, divides, and raises elements of arrays A and B element-wise.
Execution Table
StepOperationElements InvolvedResult ElementResult Array State
1Multiply element 1A(1)=1, B(1)=41*4=4[4 _ _]
2Multiply element 2A(2)=2, B(2)=52*5=10[4 10 _]
3Multiply element 3A(3)=3, B(3)=63*6=18[4 10 18]
4Divide element 1A(1)=1, B(1)=41/4=0.25[0.25 _ _]
5Divide element 2A(2)=2, B(2)=52/5=0.4[0.25 0.4 _]
6Divide element 3A(3)=3, B(3)=63/6=0.5[0.25 0.4 0.5]
7Power element 1A(1)=1, exponent=21^2=1[1 _ _]
8Power element 2A(2)=2, exponent=22^2=4[1 4 _]
9Power element 3A(3)=3, exponent=23^2=9[1 4 9]
💡 All elements processed for each operation, arrays fully computed.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9
A[1 2 3][1 2 3][1 2 3][1 2 3]
B[4 5 6][4 5 6][4 5 6][4 5 6]
C (A.*B)[][4 10 18][4 10 18][4 10 18]
D (A./B)[][][0.25 0.4 0.5][0.25 0.4 0.5]
E (A.^2)[][][][1 4 9]
Key Moments - 3 Insights
Why do we use a dot before the operator like .* instead of just *?
The dot means the operation is done element by element, not matrix multiplication. See steps 1-3 in execution_table where each element multiplies separately.
Can we use element-wise operators on arrays of different sizes?
No, arrays must be the same size or compatible for broadcasting. Here, A and B have the same size, so element-wise operations work as shown in all steps.
What happens if we forget the dot and write A * B?
Without the dot, MATLAB does matrix multiplication, which is different and may cause errors if sizes don't match. Our example uses .* to multiply elements individually (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of C after step 2?
A[4 _ _]
B[_ 10 _]
C[4 10 _]
D[1 2 3]
💡 Hint
Check the 'Result Array State' column at step 2 for C (A.*B).
At which step does the division operation finish computing all elements?
AStep 3
BStep 6
CStep 9
DStep 1
💡 Hint
Look for the last step where 'Divide element' appears in the 'Operation' column.
If we change A to [2 4 6], what will be the value of E after step 9?
A[4 16 36]
B[1 4 9]
C[2 4 6]
D[8 16 36]
💡 Hint
E is A.^2, so square each element of A. Check variable_tracker for E's final values.
Concept Snapshot
Element-wise operations use a dot before the operator: .* for multiply, ./ for divide, .^ for power.
They apply the operation to each pair of elements in arrays of the same size.
Result is a new array with each element computed separately.
Without the dot, MATLAB does matrix operations, which are different.
Always ensure arrays are compatible sizes for element-wise operations.
Full Transcript
This lesson shows how MATLAB performs element-wise operations using the dot operators: .*, ./, and .^. Starting with two arrays A and B, each element in A is combined with the corresponding element in B using the chosen operator. For example, A .* B multiplies each element of A by the matching element of B, producing a new array C. The execution table traces each step: multiplying, dividing, and powering elements one by one. The variable tracker shows how arrays C, D, and E build up their values after each step. Key points include why the dot is needed to do element-wise operations instead of matrix operations, and that arrays must be the same size. The quiz questions check understanding of intermediate results and effects of changing inputs. This visual step-by-step helps beginners see exactly how element-wise operations work in MATLAB.