the results of two functions talking to each other in a loop
1 view (last 30 days)
Show older comments
Hello Everyone...so the code all the way to ghs is correct. I wanted you to see the background. However, I am having issues picking each matrix and using it in a function and getting results from one function to feed into another function in a loop.
function [bhs,ahs,cbs]=btime(t1,t2,t3)
bhs=(t1*t2)+t3;
ahs=t1+t3;
cbs=t2./3;
end
function [abk,kba,t3]=ctime(bhs,cbs)
abk=27.3.*bhs;
kba=bhs+cbs;
t3=35*(abk+kba);
end
wqe=[1;2;3;4;5;6;7;8;9;10;11;12;13;14;15];
kli=[51;52;53;54;55;56;57;58;59;60;61;62;63;64;65];
bbc=(21:1:35)';
hsd=size(kli,1);
kas=zeros(hsd,1);
sak=zeros(hsd,1);
cbb=zeros(hsd,1);
abc=zeros(hsd,3);
for i=1:hsd
kas(i)=wqe(i);
sak(i)=kli(i);
cbb(i)=bbc(i);
end
abc(1,:) = [kas(1) sak(1) cbb(1)];
for h=2:1:hsd
abc(h,:)=[kas(h) sak(h) cbb(h)];
end
matrix{1}=abc(1,:);
for i = 2:hsd
matrix{i}=abc(1:i,:);
end
ghs=([]);
for iiv=1:1:hsd
ghs(iiv)=(matrix{1,iiv}(:,:));
t1=ghs(:,1);
t2=ghs(:,2);
[bhs(iiv),ahs(iiv),cbs(iiv)]=btime(t1(iiv),t2(iiv),t3(iiv))
[abk(iiv),kba(iiv),t3(iiv)]=ctime(bhs(iiv),cbs(iiv))
end
Final_Answer=[bhs cbs]; % The goal is to put each new bhs and cbs under the previous one.
4 Comments
Joseph Cheng
on 29 Jun 2021
must be missing something as your post still shows
for iiv=1:1:hsd
ghs(iiv)=(matrix{1,iiv}(:,:));
t1=ghs(:,1);
t2=ghs(:,2);
[bhs(iiv) ahs(iiv) cbs(iiv)]=btime(t1(iiv),t2(iiv),t3(iiv))
[abk(iiv) kba(iiv) t3(iiv)]=ctime(bhs(iiv) cbs(iiv))
end
which is missing the comma in ctime() as well as i point out that t3 which is not defined as its the output of ctime which is called AFTER you try to use t3. have you tried running your above code and debug the errors?
Answers (1)
Alan Moses
on 2 Jul 2021
Instead of using the ghs vector, you may use a temporary variable for the computation. The function 'btime' needs t3 variable to be defined before the function call. But the code only defines t1 and t2 before the function call and t3 is calculated in the next line when 'ctime' is called. The following code assumes you are using the third column of the matrix in the 'btime' function, but you have to change your algorithm if your intention is to reuse the T3 output of 'ctime' in 'btime'. Also, it is a good practice to preallocate the variables that change size on every loop.
T3 = zeros(1,hsd);
bhs = zeros(1,hsd);
ahs = zeros(1,hsd);
cbs = zeros(1,hsd);
abk = zeros(1,hsd);
kba = zeros(1,hsd);
for iiv=1:1:hsd
temp=(matrix{1,iiv}(:,:));
t1=temp(:,1);
t2=temp(:,2);
t3=temp(:,3);
[bhs(iiv),ahs(iiv),cbs(iiv)]=btime(t1(iiv),t2(iiv),t3(iiv));
[abk(iiv),kba(iiv),T3(iiv)]=ctime(bhs(iiv),cbs(iiv));
end
See Also
Categories
Find more on Loops and Conditional Statements 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!