Function for Script. I need to write a Function that selects the positive and negative numbers from a matrix. How to write this?

조회 수: 3(최근 30일)
I need to write a Function that selects the positive and negative numbers from a matrix. How to write this?

채택된 답변

dpb
dpb 2015년 1월 21일
function [p,n]=splitsigns(x)
% return positive/negative values from array x in vectors p/n, respectively
p=x(x>0);
n=x(x<0);
This one excludes 0; pick where you want those if do...
  댓글 수: 7
dpb
dpb 2015년 1월 22일
Read the help files on functions, but in general yes. Other than I'd say that p=x(x>0); and n=x(x<0); are expressions, not variables. There are no strictly local variables in those functions, only the input/output arguments (which are, of course variables just not local).

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

추가 답변(1개)

John Petersen
John Petersen 2015년 1월 21일
a = 0.5 - rand(3,3);
%Positive numbers
a(a>=0)
% negative numbers
a(a>0)
  댓글 수: 1
ricsf
ricsf 2015년 1월 21일
Thank You!
But, if I have a matrix with positive numbers and have to change it to negative and vice-versa?

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

범주

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by