How to call function repeatedly using arrays as variables?
6 次查看(过去 30 天)
显示 更早的评论
I created a function for a comparator that works correctly, it looks like this:
function Vout = myComparator(Va, Vb, Vcc, Vee)
if Va > Vb
Vout = Vcc
elseif Va == Vb
Vout = 0
else Vout = Vee
return
end
Now I am trying to find multiple "Vout"s given two seperate arrays of Va and Vb values (edit: Vcc and Vee are constant). Then I need to graph these values versus Va, Vb, and the given time data. Omitting my helper function and list of values, my code looks like this:
% Compute Outputs
B = arrayfun(myComparator, Va, Vb);
% Plot Inputs and Outputs
plot(t, Va, t, Vb, t, B);
legend('Va', 'Vb', 'B');
xlabel('time(s)'); ylabel('amplitude'); title('Non-inverting amplifier output voltage');
When I have the code for myComparator at the bottom, it gives the error messages:
"Not enough input arguments. "
"Error in solution>myComparator (line 17) if Va > Vb"
"Error in solution (line 8) B = arrayfun(myComparator, Va, Vb)"
When the myComparator code is at the top, I get no output. I feel like I am highly overthinking this and can't seem to find the issue, unless I am misunderstanding arrayfun in the documentation. Adittionally, this assigment is being completed in a canvas portal so I can't really see the full error messages.
I was told by my professor to use arrayfun or a for loop. I'm definetley a novice at coding, any info will be appreciated.
5 个评论
Duc Le
2022-11-27
I think you can use arrayfun with an anonymous function:
B = arrayfun(@(a,b) myComparator(a, b, Vcc, Vee), Va, Vb)
You'll have to define the value of Vcc and Vee first though or use actual numbers in their place in the anonymous function.
Steven Lord
2022-11-27
I think you can use arrayfun with an anonymous function:
Yes. An anonymous function is a function handle, just like a handle to a "named" function is a function handle.
f = @sin; % handle to a built-in function
class(f)
g = @(x) x.^2; % anonymous function
class(g)
h = @why; % handle to a MATLAB function file why.m
class(h)
From the documentation: "Many MATLAB® functions accept function handles as inputs so that you can evaluate functions over a range of values. You can create handles either for anonymous functions or for functions in program files. The benefit of using anonymous functions is that you do not have to edit and maintain a file for a function that requires only a brief definition."
采纳的回答
James Tursa
2022-11-15
编辑:James Tursa
2022-11-15
There are other ways do to this, but to answer your question about how to turn the code into a loop, use indexing based on the number of elements of your array. E.g., added the for-loop and the (k) indexing:
Vout = zeros(size(Va)); % pre-allocate
for k=1:numel(Va) % added the for-loop
if Va(k) > Vb(k)
Vout(k) = Vcc; % add semi-colons so the result doesn't print to screen
elseif Va(k) == Vb(k)
Vout(k) = 0;
else
Vout(k) = Vee;
end
end % end for-loop
This assumes Va and Vb have the same number of elements. And put the return statement outside the loop.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!