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

3 vues (au cours des 30 derniers jours)
I need to write a Function that selects the positive and negative numbers from a matrix. How to write this?

Réponse acceptée

dpb
dpb le 21 Jan 2015
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 commentaires
ricsf
ricsf le 22 Jan 2015
Ok! So, like you have writen in the first one, just to have no doubts in this.
I've the declaration of the function, with the: Output Arg (n,p) and the Input Arg (x)
And the Variables of the funtion are: p=x(x>0); n=x(x<0);
Am I thinking correctly?
dpb
dpb le 22 Jan 2015
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).

Connectez-vous pour commenter.

Plus de réponses (1)

John Petersen
John Petersen le 21 Jan 2015
a = 0.5 - rand(3,3);
%Positive numbers
a(a>=0)
% negative numbers
a(a>0)
  1 commentaire
ricsf
ricsf le 21 Jan 2015
Thank You!
But, if I have a matrix with positive numbers and have to change it to negative and vice-versa?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by