help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: writing functions that accept arrays/(matrcies?)


From: David Grundberg
Subject: Re: writing functions that accept arrays/(matrcies?)
Date: Mon, 09 Nov 2009 08:49:59 +0100
User-agent: Thunderbird 2.0.0.21 (X11/20090302)

Christopher Harvey wrote:
Hello list,
I'm pretty new to octave and I have a few questions.
I'll try to keep them in list form as much as possible.

Are ranges, arrays and matrices the same thing in octave? (ie the same data type?)

Ranges (e.g. 1:4), cellarrays ({}) and matrices ([1 2 3]) are all arrays. As an example, all arrays can be indexed with (), which returns an array of the same type. But cellarrays have a different class from matrices, so they store different things.

Note that ranges and matrices both return "double" from the class() function, so they are compatible in every way. If you use typeinfo(), you can see what internal representation Octave chosen. typeinfo() is more for helping C++ developers debug than it is useful for m-file developers.

If I write a function that accepts arrays (ranges?) like sin([1:10]) how can I check to make sure that it's not a matrix that's actually passed in?
How do other built in functions handle this in octave?

You cannot make a difference between ranges and double matrices, except if you cheat and use typeinfo(). But I consider that bad style, because ranges and matrices belong to the same class.

If you have a lot of parameters and want to have error checking, you can use is*() functions, which checks the class. That way you are not dependent on the internal representation in Octave.

I would like to write a function that can accept and produce arrays, can I get a template that can do this? ie: myFunction([1:10], 1, 2, 3)
that returns [x1, x2, x3....... x10]

Something like this

function y = linearfunc(x, k, m)
 y = x .* k + m;
endfunction

thanks for clearing this up,
Chris.
_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave



reply via email to

[Prev in Thread] Current Thread [Next in Thread]