Please HELP!! Am I nuts?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I define a function and save it:
function [new_x, new_y] = TestArray(x, y)
new_y = y.^2;
new_x = x;
end
At cursor I type:
x = 1:10
y = 1:10:30
Answer = TestArray(x, y)
I get:
Answer =
1 2 3 4 5 6 7 8 9 10
How on earth is this possible? I am feeling tired but I can only assume I am going crazy, tiredness cannot be a reason for this level stupidity?! Shouldn't i return an matrix with size [2x10]?
0 Commenti
Risposta accettata
Image Analyst
il 7 Nov 2012
[new_x, new_y] in the function definition, or when you actually call this function, does not mean that you will get one variable out with new_x in column 1 and new_y in column 2, like you would if you did this:
c = [columnVector1 columnVector2];
It behaves differently. When calling or defining a function, it means that you get two variables returned and they are separate - not concatenated. If you specify only one, it will give you the left most return variable and any to the right of it are "thrown away" because you did not supply a variable in your calling routine to accept them. I hope that explains it.
0 Commenti
Più risposte (2)
Friedrich
il 7 Nov 2012
Modificato: Friedrich
il 7 Nov 2012
Hi,
this behavior is correct. In a function the output arguments aren't concatinated. They are returned as several outputs.
Try calling your function TestArray as
[Answer, Answer2] = TestArray(x, y)
If you want to have a 2x10 Matrix as output, then you need to concatinate it yourself inside the function.
0 Commenti
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!