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

a) Create matrix B wich has 5 rows and 5 columns using magic function. Write b)

ID: 3874858 • Letter: A

Question

a) Create matrix B wich has 5 rows and 5 columns using magic function. Write b) Write a single MATLAB statement to assign the elements of the last column to c) With the aid of find function, write a series of MATLAB statements to detemine down the elements of B in the file Qtn4.m as a comment variable lastColumn. Write down the value of lastColumn as a comment the average of all elements in matrix B which are multiples of 2 and assign the answer to variable avgdiv2. Write down the value of avgdiv2 as a comment d) Write a series of MATLAB statements to calculate how many numbers in matrix B which are between 6 and 33 (a inclusive) and assign the answer to variable numlnRange. Write down the value of numInRange as a comment

Explanation / Answer

matlab Script:--

edit Qtn41.m
st='% ';
B=magic(5); %5X5 matrix B, with magic
fileID = fopen('Qtn41.m','a');   %file open with Qtn4.m
for i=1:5
    fprintf(fileID,"%s %d %d %d %d %d ",st,B(i,:));    %print matrix B to file
end
lastColumn=B(1:5,5);    %last column of B to variable lastColumn
for i=1:5
    fprintf(fileID,' %s %d',st,lastColumn(i));   %print lastColumn to file
end
k=1;
for i=1:5
    for j=1:5
        if (mod(B(i,j),2)==0)   %test each element of B is multiple of 2 or not
            div2(k)=B(i,j); %if yes assign the element to div2
            k=k+1;
        end
    end
end
avgdiv2=mean(div2); %average of div2 and assign to avgdiv2
fprintf(fileID,' %s %d ',st,avgdiv2); %print avgdiv2 to file

k=1;
for i=1:5
    for j=1:5
        if (B(i,j)>=6 && B(i,j)<=33)    %test each element of B is multiple of 2 or not
            range(k)=B(i,j);    %if yes assign to range
            k=k+1;
        end
    end
end
[r, numInRange]=size(range); %find size of range
fprintf(fileID,' %s %d ',st,numInRange);

output:-- (file Qtn4.m)

% 17 24 1 8 15
% 23 5 7 14 16
% 4 6 13 20 22
% 10 12 19 21 3
% 11 18 25 2 9

% 15
% 16
% 22
% 3
% 9

% 13


% 20