How to execute multiple commands at once

I think this should be fairly basic but I want to make a function where I put an input array, the function does multiple simples commands and creates an output array
For example
Somthing like
function('input','output') %changes input to output using the following commands at once to the array
output(:,1)=input(:,1)*0.72+74;
output(:,3)=input(:,3)*0.39+170;
output(:,4)=input(:,3)*0.78+57;
output(:,5)=input(:,3)*1.2+0.46;
output(:,9)=input(:,3)*1.1+26;
output(:,13)=input(:,3)*0.52+130;
output(:,14)=input(:,3)*0.63+97;
output(:,15)=input(:,3)*0.66+91;
output(output(:,9)==26,9) = 0; %changes everything that is y intercept to 0
output(output(:,5)==0.460,5) = 0; %changes everything that is y intercept to 0
Thanks!
MATLAB Version: 9.3.0.713579 (R2017b)

Réponses (1)

Fangjun Jiang
Fangjun Jiang le 21 Fév 2019
Modifié(e) : Fangjun Jiang le 21 Fév 2019
You just need to change the first line to below and save it as MyCal.m.
function output=MyCal(input)
You function name would be called MyCal. You use it like
MyCal(magic(15))
type "doc function" to learn how to define function

6 commentaires

Thank you
I have written the following but it's not working?
function output=simu(input)
input(:,1)=input(:,1)*0.72+74;
input(:,3)=input(:,3)*0.39+170;
input(:,4)=input(:,3)*0.78+57;
input(:,5)=input(:,3)*1.2+0.46;
input(:,9)=input(:,3)*1.1+26;
input(:,13)=input(:,3)*0.52+130;
input(:,14)=input(:,3)*0.63+97;
input(:,15)=input(:,3)*0.66+91;
input(input(:,9)==26,9) = 0;
input(input(:,5)==0.460,5) = 0;
end
Walter Roberson
Walter Roberson le 21 Fév 2019
Modifié(e) : Walter Roberson le 21 Fév 2019
We recommend against naming a variable input as doing so conflicts with the input() function, and confuses readers of your code.
You should avoid using floating point equality tests.
As you are adding 0.46 in the 4th line of your assignments, then provided that MATLAB parses 0.46 and 0.460 as being the same value (not guaranteed), then the == could work, but only in the situation where input(:,3) was exactly 0. It might work in this case, but you would have to research IEEE standards in detail to be sure that it will work in future. It is far more robust to test for equality with a tolerance or to directly test input(:,3) for exact 0 .
William Campbell
William Campbell le 21 Fév 2019
Walter
Thanks for the response
I wasn't going to, I was just using it as an example. and input(:,3) is exactly zero
Do you have any advice on how to name the output?
Is there a way you can make a function to change m to n in the format Mycalc('m','n')
thanks
Walter Roberson
Walter Roberson le 21 Fév 2019
I do not understand your question about changing m to n ??
Sorry maybe I should have explained more clearly;
I have a data set a.x which has 18 columns and 10273 rows
I want to edit the columns using the following
a.x(:,1)=a.x(:,1)*0.72+74;
a.x(:,3)=a.x(:,3)*0.39+170;
a.x(:,4)=a.x(:,3)*0.78+57;
a.x(:,5)=a.x(:,3)*1.2+0.46;
a.x(:,9)=a.x(:,3)*1.1+26;
a.x(:,13)=a.x(:,3)*0.52+130;
a.x(:,14)=a.x(:,3)*0.63+97;
a.x(:,15)=a.x(:,3)*0.66+91;
a.x(a.x(:,9)==26,9) = 0;
a.x(a.x(:,5)==0.460,5) = 0;
I have been doing this normally but doing each command seperately.
I was wondering if there was way of doing all commands at once to a.x and calling the new file b.x?
Thanks
Walter Roberson
Walter Roberson le 22 Fév 2019
I suspect it can be coded as a matrix multiplication with a 18 x 19 matrix, starting from eye(18,19) and setting specific coefficients. However the setting of the coefficients would be a nuisance. I guess you could use sparse() to put them in in a compact form, but it would not be clear.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by