2. Write a MATLAB function that takes a matrix, a row number and a column number
ID: 3710645 • Letter: 2
Question
2. Write a MATLAB function that takes a matrix, a row number and a column number. Beginning with the row number passed to the function, scan down the column passed to the function and return the row number that contains the largest absolute value in the column. For example, if your matrix is [(1 9 4 5:11 -2 3 0:0 -6 5 4:3 5 7 -3)] and you pass row 2, column 2, the function should return 3. Row 3 has the largest absolute value in column 2 including and below row 2. Use loops for the program. Do not use the : operator.
Explanation / Answer
PLEASE REFER BELOW STEPS
1) CREATE FILE largest_column.m AND PASTE BELOW CODE
function r = largest_column(A,row,col)
[row_A,col_A] = size(A) %size of matrix
A = abs(A); %converting matrix to absolute values
max_ele = abs(A(row,col)); %taking first element as referrence to calculate max element
for i = row:row_A
if A(i,col) > max_ele %if next row element is greater then save it's row number
max_ele = A(i,col) ;
r = i;
end
end
2) CREATE test.m AND PASTE BELOW CODE
close all
clear all
clc
A = [1 9 4 5;11 -2 3 0;0 -6 5 4;3 5 7 -3]
r = largest_column(A,2,2)
PLEASE RUN test.m AND YOU WILL GET BELOW OUTPUT
A =
1 9 4 5
11 -2 3 0
0 -6 5 4
3 5 7 -3
row_A =
4
col_A =
4
r =
3
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.