Write function that returns true if (inputmatrix > 0)

I suppost Write a function with given matrix A returns matrix P where element P(i,j) = true if the corresponding elements in (A(i,i)>0) and false otherwise. I don't really udnersatnd the queation, why does it say P(i,j) and then A(i,i)?
it is hinted that we can use nested for-loop but as i stated above i don't really get the question so it makes it hard to execute the code. Any kind soul that can help me get a start?
function P = positive(A)
for
for
end
end
end

 채택된 답변

Can Atalay
Can Atalay 2021년 10월 17일
편집: Can Atalay 2021년 10월 17일
The (i,i) part of the problem is probably a typo, can you ask for clarafication and report back? I don't see how that would be useful.
If they're expecting you to output 1 at J(i,j) if A(i,j)>0 and output 0 at J(i,j) if otherwise, then you could simply write;
A = [1,2,3;1,2,0;1,2,5];
J = check_positivity(A)
function [resulting_logical_matrix] = check_positivity(A)
resulting_logical_matrix = A>0; % this directly calculates the positivity of all elements
end
If they need you to output 1 at J(i,j) if A(i,i)>0 and output 0 at J(i,j) if otherwise, then you need too look at the diagonal elements of A only;
A = [1,2,3;1,2,0;1,2,5];
J = check_positivity(A)
function [resulting_matrix] = check_positivity(A)
[row1,col1] = size(A) ; % good practice to always use size() instead of length()
resulting_matrix = zeros(row1,col1); % good practice to always preallocate when possible
for ii1=1:row1
for ii2 = 1:col1
if A(ii1,ii1)>0 % if the diagonal element at that row is >0
resulting_matrix(ii1,ii2)=1; % elements in that row are 1
else % otherwise
resulting_matrix(ii1,ii2)=1; % elements in that row are 0
end
end
end
end
Hope this was helpful

댓글 수: 4

The image above highlights the diagonal elements of a matrix ( A(i,i) means i=j )
The question is asking you to make all elements in the first row 1 if the diagonal in the first row is >0 , and make all elements in the first row 0 if the diagonal in the first row is not >0, and repeat for the second row, third row etc. until it's over.
Also note I'm using ii1 and ii2 instead of i and j because they're used frequently in complex numbers to avoid confusion
Can Atalay
Can Atalay 2021년 10월 17일
편집: Can Atalay 2021년 10월 17일
Also, while the first function I wrote isn't wrong, the second function is probably going to give you an error if the matrix isn't square, so you might need to check if A(ii1,ii1) exists. If it doesn't, make an exception using the
try
command. Read the documentation on try and attempt a solution that way. If that's too much, add another condition to the if statement to check if A(ii1,ii1) exists or modify the for loops' ranges to match the smallest dimension of the matrix (or write a single for loop that fills all elements of the row by only checking the diagonal and iterates from 1 to n where n is min(m,n) for a mxn matrix). If none works answer me and I can give you a complete solution
thank you :)
No problem!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2021년 10월 17일
A = rand(5) ;
P = A > 0.5
P = 5×5 logical array
1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2021년 10월 17일

댓글:

2021년 10월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by