Filename: MAE1090_HW7_2.m Write an m-file that determines the row and column ind
ID: 2086534 • Letter: F
Question
Filename: MAE1090_HW7_2.m Write an m-file that determines the row and column indices for all non-zero elements in a matrix, x. Row indices should be stored to the variable i, and column indices should be stored to the variable j. For the purpose of testing and grading your m-file: ? DO NOT issue a clear command at the start of your m-file ? DO NOT define the matrix x within your m-file. That vector should exist in the workspace prior to executing your m-file. ? Do not display i and j. This will be done by the script the grader uses to test your m-file. ? Variable names (x, i, and j) must be exactly as specified (lower case).
Explanation / Answer
x=randi([0,10],[3,3]); %create x just to check code
[m n]=size(x);
k=1
for p=1:m
for q=1:n
if x(p,q)~=0
i(k,1)=p; % row havinig non zero element
j(k,1)=q; % column having non zero element
k=k+1
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.