Function to Vector
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello all,
I am currently attempting to have my function be able to output a vector into the workspace. Currently i have tried to something like this (simplified a lot but this is the jist of it)
for i = 1:10
a(i) = crr(x)
end
where a(i) is supposed to be my output vector and crr is my function, x is some vector that crr acts on. crr just outputs one numerical value. even when i do something like a = crr(x) i get an error message that says "too many output values"
if anybody knowss a way that allows crr to output a vector into the workspace, your help would be much appreciated.
dustin
2 comentarios
the cyclist
el 19 de En. de 2012
Can you post a (possibly simplified) version of the function crr() that give the error message?
Also, you could use the debugger ("dbstop if error") to halt execution when the error occurs, to see exactly what your variables look like.
Respuestas (3)
Andrew Newell
el 19 de En. de 2012
It is very natural for functions to output vectors in MATLAB. See the documentation for function for some examples. Here is a very simple example:
function y = crr(x)
y = x.^2;
Store this in a file crr.m, and run:
x = 1:10;
a = crr(x)
0 comentarios
Walter Roberson
el 19 de En. de 2012
That error would occur if your function crr() was not defined as returning any values. Andrew's Answer shows what the declaration should look like.
0 comentarios
Honglei Chen
el 19 de En. de 2012
If I understand your question correctly, you need to do
for i = 1:10
a(i) = crr(x(i))
end
But you could try arrayfun, e.g.,
a = arrayfun(@crr,x)
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!