Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab code required [A]Array 1 has 20 values [B] Array 2 has 21 values I need t

ID: 3922579 • Letter: M

Question

Matlab code required
[A]Array 1 has 20 values [B] Array 2 has 21 values
I need to element by element do the following:
A (1) - B (1)
A (2) - B (2)
And somehow be able to discount the last B value
So my last calc will be A (20) - B (20)
Using A for loop with storing values and a counter
Matlab code required
[A]Array 1 has 20 values [B] Array 2 has 21 values
I need to element by element do the following:
A (1) - B (1)
A (2) - B (2)
And somehow be able to discount the last B value
So my last calc will be A (20) - B (20)
Using A for loop with storing values and a counter

[A]Array 1 has 20 values [B] Array 2 has 21 values
I need to element by element do the following:
A (1) - B (1)
A (2) - B (2)
And somehow be able to discount the last B value
So my last calc will be A (20) - B (20)
Using A for loop with storing values and a counter

Explanation / Answer

Here is the code:

Remeber in matlab Arrays are two dimentioal, Vectors are single dimensional

% Here A with 20 values

A = [11 20 32 4 5 6 7 8 9 10 12 45 8 7 4 6 3 1 2 21];

% B with 21 values
B = [11 20 32 4 5 6 7 7 9 10 12 45 8 7 4 6 3 1 2 20 12];

% A counter variable for arrays

count=1

% for loop for 21 times
for i=1:21

% checking if it is 21 th iteration
if i==21

% if true then breakig the loop
break

% else calculating the value A(count)-B(count) then incrementing the counter variable
else
A(count)-B(count)
count=count+1;

% End of if
end

% End of for loop
end

Ouput:

count =1

ans = 0

ans = 0

....

...

...

ans = 1