Help in Matlab please. Need to write a code for this function. function [ X ] =
ID: 2321881 • Letter: H
Question
Help in Matlab please. Need to write a code for this function.
function [ X ] = myTightenImage( X )
a. INPUTS: i. A 2D matrix X
b. FUNCTIONALITY: i. This function looks through each row of X, and removes it from X if it only contains 0s.
ii. This function looks through each column of X, and removes it from X if it only contains 0s.
c. OUTPUTS:
i. An updated version of X in which every row and column contains at least one non-zero number
d. HINTS:
i. You can remove rows 7 through 8 of X with this statement: X(7:8, :) = [];
ii. You can remove cols 2, 4, 5, 9 of X with this statement: X(:, [2 4 5 9) = [];
iii. You can find which columns of X have non-zero numbers with this statement: any(X, 1);
iv. You can find which rows of X have non-zero numbers with this statement: any(X, 2);
v. You can find which rows of X have all zeros with this statement: ~any(X, 2);
e. TEST CASES:
>> A = [0 0 0 0 0; 1 2 3 4 5; 6789 9; 2 4684]; >>myTightenImage (A) ans = >> A = [0 0 0 0 0; 0 1 2 3 4 000 1 0 0 2 4 6 0 >>myTightenImage (A) ans = 0Explanation / Answer
Matlab Code
function [ A ] = myTightenImage( A )
B = any(A,2);
for i=1:length(B)-1
if(B(i)==0)
A(i,:)=[];
end
end
C = any(A,1);
for i=1:length(C)-1
if(B(i)==0)
A(:,i)=[];
end
end
end
Sample Output
A = [0 0 0 0 0; 0 1 2 3 4; 0 0 0 1 0; 0 2 4 6 0];
myTightenImage(A)
ans =
1 2 3 4
0 0 1 0
2 4 6 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.