In Matlab write a function function [ X ] = myTightenImage( X ) a. INPUTS: i. A
ID: 3697440 • Letter: I
Question
In Matlab write a 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) = []; i
ii. 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; 6 7 8 9 9; 2 4 6 8 4];
>> myTightenImage(A) ans = 12345 67899 24684
>> 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 ]
Explanation / Answer
% Function start here
function y = myTightenImage(x)
%remove rows if all row value is zero
x( ~any(x,2), : ) = [];
%remove columns if all col value is zero
x( :, ~any(x,1) ) = [];
%return final matrix here
y = x;
end
% Function start here
% define matrix here
A = [0 0 0 0 0; 1 2 3 4 5; 6 7 8 9 9; 2 4 6 8 4];
%A = [0 0 0 0 0; 0 1 2 3 4; 0 0 0 1 0; 0 2 4 6 0];
% call function here
myTightenImage(A)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.