MATLAB: Write a function called matrix_problem that takes as input an array name
ID: 3028643 • Letter: M
Question
MATLAB: Write a function called matrix_problem that takes as input an array named X that is a matrix or a vector. The function identifies those elements of X that are greater than the sum of their two indexes. For example, if the element X(2,3) is 6, then that element would be identified because 6 is greater than 2 + 3. The output of the function gives the indexes of such elements found in row-major order. It is a matrix with exactly two columns. The first column contains the row indexes, while the second column contains the corresponding column indexes. For example, the call ind = matrix_problem([1 4; 5 2; 6 0]), will make ind equal to [1 2; 2 1; 3 1]. If no such large element exists, the function returns an empty array.
Explanation / Answer
main program :
clc;
clear all;
close all;
A=[ 1 8 2;2 0 1; 3 8 4]; %%% Defining input vector
ind=matrix_problem(A)
%%%%%%%%%%% Function body %%%%%%%%
function x=matrix_problem(B)
[a,b]=size(B);
k=0;
for i=1:a
for j=1:b
if B(i,j)>(i+j)
k=k+1;
x(k,1:2)=[i,j];
end
end
end
if (k==0)
x=0;
end
Output:
ind =
1 2
3 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.