Reaching the element of an array passed to a function
Show older comments
I'm trying to create a function y=f(x) composed of two different parts for x < constant C & x >= C. When one pass an array A to f(x), the return would be an array B where Bij=f(Aij) How do I create a valid "if statement" in the function f? Below is the code I'm using (say I want to create a step function) What happened is since A is an array, all statement would be false Is there a way to call the "current" element in array A?
function y=f(A)
if A >= C
y=0
elseif A<C
y=1
end
end
Accepted Answer
More Answers (1)
Image Analyst
on 4 Mar 2017
Edited: Image Analyst
on 4 Mar 2017
Why not just do
B = A < C;
Why bother making a function at all when a simple one-liner will do it?
You're either over-thinking it or I'm not understanding what you want. So please post examples with x, y, A, B, and/or C so we can see what you want.
1 Comment
Hsin-Hua Wang
on 7 Mar 2017
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!