Function to Vector
32 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
the cyclist
2012 年 1 月 19 日
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.
回答 (3 件)
Andrew Newell
2012 年 1 月 19 日
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 件のコメント
Walter Roberson
2012 年 1 月 19 日
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 件のコメント
Honglei Chen
2012 年 1 月 19 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!