How do I use function for a function which has a matrix with variable index?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
function[PacksA,PacksB,ContA,ContB,AR,BR,fA,fB]=OutAR_NotOutBR(i,Max,AR,BR,fB,fA,ContB)
if AR<Max
%PacksB, PacksA, ContB, ContA are vectors that vary according to the operation
PacksB=3; fB=fB+1;
PacksA=2; ContA(AR+1)=i; AR=AR+1;
if BR==1
PacksB(ContB(1))=4; fB=fB+1;
ContB(1)=0; BR=BR-1;
elseif BR>=2
PacksB(ContB(1,1:2))=4; fB=fB+2;
if BR==2
ContB=0;
else
ContB(1)=ContB(3); ContB(1,2:3)=0;
end
BR=BR-2;
end
elseif AR==Max
PacksA=3; fA=fA+1;
PacksB=3; fB=fB+1;
if BR>0
PacksB(ContB)=4; fB=fB+BR;
ContB=0;
BR=0;
end
%Hi. What's up? Is there someone here who can help me with this error?
%Error in ==> OutAR_NotOutBR at 2 %if AR<Max
4 Comments
Geoff Hayes
on 17 Nov 2020
Fidele - what can you tell us about the input parameters? What is AR and what is Max? Is the problem the missing end for your last if statement?
if BR>0
PacksB(ContB)=4; fB=fB+BR;
end % <--- should this be here?
ContB=0;
BR=0;
Fidele Adanvo
on 17 Nov 2020
Edited: Fidele Adanvo
on 17 Nov 2020
Geoff Hayes
on 18 Nov 2020
You may need to use the MATLAB debugger to step through the code to get an idea of what is going wrong. I'm also not clear on your comment:
This code is not all correct because when BR == 3
PacksB (ContB (1,3)) = ~ 4
ContB (1) ~ = 0
and BR=1
Why?
Fidele Adanvo
on 18 Nov 2020
Answers (1)
Geoff Hayes
on 18 Nov 2020
Fidele - since your function signature is
function [a,b,c] = trial_function(num)
then you need to assign values to each of the three output variables which you are not currently doing
if num==2
a=10;
b=5
elseif num==4 % <---- note this should be elseif rather than else if
c=100;
end
The easiest solution is to just assign default values to these three variables as
a = 0;
b = 0;
c = 0;
if num==2
a=10;
b=5
elseif num==4 % <---- note this should be elseif rather than else if
c=100;
end
But is the above code really what you want? Do you want to assign different values to a and b and c if num is 2 or 4 or any other integer?
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!