I want to use a for loop inside a function, where the for loop contains a subfuction. Each loop must store the variable value and make a matrix of all values, but I get 0s :(
古いコメントを表示
function [a] = Main(Coordinates)
a=5;
for n=1:a
Coordinates=3+n;
a= test(n,Coordinates)
end
end
function [A, B]= test(n,Coordinates)
A(n)= Coordinates*3;
B(n)= A(n)*Coordinates*6;
end
3 件のコメント
Nikolas Katsantonis
2022 年 6 月 17 日
Nikolas Katsantonis
2022 年 6 月 17 日
Geoff Hayes
2022 年 6 月 17 日
That makes sense, right? Look at this code
function [A, B]= test(n,Coordinates)
A(n)= Coordinates*3;
B(n)= A(n)*Coordinates*6;
end
You are creating a new A and B whenever the test function is called. These new variables/arrays won't have the history from previous calls to this function. So you are always returning arrays of zeros except for the nth value which is set in this function call.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!