the results of two functions talking to each other in a loop

조회 수: 4(최근 30일)
DARLINGTON ETAJE
DARLINGTON ETAJE 2021년 6월 29일
댓글: DARLINGTON ETAJE 2021년 7월 2일
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
DARLINGTON ETAJE
DARLINGTON ETAJE 2021년 6월 29일
Thanks. I have adjusted the code...my intention is to calculate T3 from one function and input it into the next function...help me out please

댓글을 달려면 로그인하십시오.

답변(1개)

Alan Moses
Alan Moses 2021년 7월 2일
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
  댓글 수: 1
DARLINGTON ETAJE
DARLINGTON ETAJE 2021년 7월 2일
Alan, thanks for the attempt. I am grateful.
see the final line of my code.
Final_Answer=[bhs cbs]; % The goal is to put each new bhs and cbs under the previous one.
You will realize that matrix contains several matrices with increasing sizes. so the eventual answer for bhs and cbs are meant to increase on every loop

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by