How to define a matrix valued function in Matlab/Octave -- return value syntax

Assad Ebrahim

I'm trying to verify some matrix rotation calculations, and thought it would be easiest to drop into Octave (Matlab) and define a simple function to do this. But I am stymied by some syntax difficulties that I think are due to returning a matrix (multiple values).

Ideally, I'd like to be able to key in something simple, where I can choose the rotation degree and the point to be rotated, for example:

rot(45)*[1 0]'  % rotate unit vector by 45 degrees

I'd expect to get back the correct answer: 0.70711 0.70711

However, this means that rot(45) must return a matrix in a form that can then be multiplied immediately by the vector.

To do this I've defined the following function, using the [ R ] syntax to indicate multiple return values:

 function [ R ] = rot(th_deg)
    [ cos(th_deg * pi/180)  -sin(th_deg * pi/180) ;
      sin(th_deg) * pi/180)  cos(th_deg * pi/180)   ]
 end

Calling function rot(45) by itself works fine and shows the correct 2x2 rotation matrix.

But trying to use this rotation matrix returned value in the further multiplication yields the warning message: warning: some elements in list of return values are undefined

Any idea what's going wrong?

Thanks,

Floris

That is because as written, calling your function will print the result but not return it.

Try

function [ R ] = rot(th_deg)
  R = [ cos(th_deg * pi/180)  -sin(th_deg * pi/180) ;
  sin(th_deg) * pi/180)  cos(th_deg * pi/180)   ];
end

Note the ; to suppress output of the result when the function is called, and setting R= in order to return a result.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to define method with self-referencing Type return value

分類Dev

Define macro value in function

分類Dev

How to return value from debounced function in javascript?

分類Dev

How to correctly return value from function?

分類Dev

How to capture function return value with if condition

分類Dev

How to unwrap return value at function call?

分類Dev

How to return value from a child process in a function?

分類Dev

How to get a return value from a connect function

分類Dev

How to return value from an extended casperjs function?

分類Dev

How to process return value of main() function?

分類Dev

golang recover return value syntax

分類Dev

How to define a function in dplyr?

分類Dev

How to get compile time error checking with real valued function arguments?

分類Dev

How to define Java function's return type and parameters, using subclass or superclass?

分類Dev

Define a dynamic array in a function, then return the array element. How to release array's memory?

分類Dev

How to return one and only one value from a PowerShell function?

分類Dev

How can I return a function that uses the value of a variable?

分類Dev

How to get the single return value from mysql stored function in php

分類Dev

Typescript / TSLint : how to call typeof on function return value?

分類Dev

How can I print the return value of a JavaScript function in HTML?

分類Dev

How to return a value in a JS asynchronous callback function - gapi

分類Dev

How can I return a value from a parent function in Node?

分類Dev

PHP: how to return counter of value from recursive function?

分類Dev

How to make the return value of a function user input-able in python

分類Dev

How to properly return value from a data inside the function

分類Dev

How to return a value from a async function in array.map in javascript?

分類Dev

How to make a recursive fib-function return the correct value with memoization

分類Dev

Why Javascript docs define function's parameter as a nested array in syntax?

分類Dev

How to define the function in curve fitting?

Related 関連記事

  1. 1

    How to define method with self-referencing Type return value

  2. 2

    Define macro value in function

  3. 3

    How to return value from debounced function in javascript?

  4. 4

    How to correctly return value from function?

  5. 5

    How to capture function return value with if condition

  6. 6

    How to unwrap return value at function call?

  7. 7

    How to return value from a child process in a function?

  8. 8

    How to get a return value from a connect function

  9. 9

    How to return value from an extended casperjs function?

  10. 10

    How to process return value of main() function?

  11. 11

    golang recover return value syntax

  12. 12

    How to define a function in dplyr?

  13. 13

    How to get compile time error checking with real valued function arguments?

  14. 14

    How to define Java function's return type and parameters, using subclass or superclass?

  15. 15

    Define a dynamic array in a function, then return the array element. How to release array's memory?

  16. 16

    How to return one and only one value from a PowerShell function?

  17. 17

    How can I return a function that uses the value of a variable?

  18. 18

    How to get the single return value from mysql stored function in php

  19. 19

    Typescript / TSLint : how to call typeof on function return value?

  20. 20

    How can I print the return value of a JavaScript function in HTML?

  21. 21

    How to return a value in a JS asynchronous callback function - gapi

  22. 22

    How can I return a value from a parent function in Node?

  23. 23

    PHP: how to return counter of value from recursive function?

  24. 24

    How to make the return value of a function user input-able in python

  25. 25

    How to properly return value from a data inside the function

  26. 26

    How to return a value from a async function in array.map in javascript?

  27. 27

    How to make a recursive fib-function return the correct value with memoization

  28. 28

    Why Javascript docs define function's parameter as a nested array in syntax?

  29. 29

    How to define the function in curve fitting?

ホットタグ

アーカイブ