Info

此问题已关闭。 请重新打开它进行编辑或回答。

For loop is not working

1 次查看(过去 30 天)
Sudhanshu Soni
Sudhanshu Soni 2020-8-4
关闭: MATLAB Answer Bot 2021-8-20
I started with matlab for neural networks but when i use for loop, it would only return me the output for last element
code is:
load('Trained_network.mat');
N=3;
for i = 1:N
transposed_Input = input(i,:)';
weighted_sum=Weight*transposed_Input;
output=sigmoid(weighted_sum);
end
It returns output only for Nth element while It should return for 1:N

回答(1 个)

Cris LaPierre
Cris LaPierre 2020-8-4
A loop runs the same code over and over. That means you overwrite your variables each time. As you've written it, the result will only be the value of output from the final loop. If you want to capture the results from each loop, use indexing.
for a = 1:5
output(a) = a;
end
The final value of output in this example is
output =
1 2 3 4 5

Community Treasure Hunt

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

Start Hunting!

Translated by