Info
此问题已关闭。 请重新打开它进行编辑或回答。
How do I use function for a function which has a matrix with variable index?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2020-11-18
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?
回答(1 个)
Geoff Hayes
2020-11-18
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?
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!