If v=-10:1:10, why isn't the v>0 case triggered?

1 view (last 30 days)
I am trying to understand if I call my function:
v=-10:0.1:10;%voltage across transistor
i = logic(v,25)
And my function is:
function i = logic(v,T)
if(v>=0)%forward
if(T==25)
i1="T=25, forward";
end
if(T==150)
i1="T=150, forward";
end
else%reverse v<0
if(T==25)
i2="T=25, backward";
end
if(T==150)
i2="T=150, backward";
end
end
i=i1+i2;
I thought i would be "T=25, backwardT=25, forward". But instead MATLAB gave me an error saying "Unrecognized function or variable 'i1'." So I think the if(v>=0) case wasn't even executed. But v=-10:1:10 contains positive values as well.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Nov 2023
Edited: Walter Roberson on 2 Nov 2023
You are passing the entire vector v into logic(). Inside logic() you are testing if(v>=0) -- so you were testing the entire vector at the same time in a single if statement. But in MATLAB, if CONDITION is equivalent to if ALL(CONDITION(:)~=0) -- that is, the body of the if statement is executed only if all of the values being tested at the same time are non-zero. When the test involves a logical operator, that is equivalent to saying that the body is only executed if all of the tests on the vector came out true. But there are negative entries in the vector so testing > 0 is going to fail.
MATLAB never automatically tests each individual entry in a vector or array to somehow gather the results up. if is not a loop.
v=-10:0.1:10;%voltage across transistor
i = arrayfun(@(V)logic(V,25), v)
i = 1×201 string array
Columns 1 through 9 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 10 through 18 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 19 through 27 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 28 through 36 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 37 through 45 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 46 through 54 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 55 through 63 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 64 through 72 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 73 through 81 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 82 through 90 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 91 through 99 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 100 through 109 "T=25, backward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 110 through 119 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 120 through 129 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 130 through 139 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 140 through 149 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 150 through 159 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 160 through 169 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 170 through 179 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 180 through 189 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 190 through 199 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 200 through 201 "T=25, forward" "T=25, forward"
function i = logic(v,T)
i1 = "";
i2 = "";
if(v>=0)%forward
if(T==25)
i1="T=25, forward";
end
if(T==150)
i1="T=150, forward";
end
else%reverse v<0
if(T==25)
i2="T=25, backward";
end
if(T==150)
i2="T=150, backward";
end
end
i=i1+i2;
end
  1 Comment
Walter Roberson
Walter Roberson on 2 Nov 2023
Edited: Walter Roberson on 2 Nov 2023
If you do not want to arrayfun to call the function repeatedly, then you need to loop inside the function.
v=-10:0.1:10;%voltage across transistor
i = logic(v,25)
i = 1×201 string array
Columns 1 through 10 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 11 through 20 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 21 through 30 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 31 through 40 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 41 through 50 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 51 through 60 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 61 through 70 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 71 through 80 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 81 through 90 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 91 through 100 "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" "T=25, backward" Columns 101 through 110 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 111 through 120 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 121 through 130 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 131 through 140 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 141 through 150 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 151 through 160 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 161 through 170 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 171 through 180 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 181 through 190 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Columns 191 through 200 "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" "T=25, forward" Column 201 "T=25, forward"
function i = logic(V,T)
numv = numel(V);
i = strings(1, numv);
for K = 1 : numv
v = V(K);
i1 = compose("<no match for v=%g T=%g>", v, T);
if(v>=0)%forward
if(T==25)
i1="T=25, forward";
end
if(T==150)
i1="T=150, forward";
end
else%reverse v<0
if(T==25)
i1="T=25, backward";
end
if(T==150)
i1="T=150, backward";
end
end
i(K) = i1;
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!