Effacer les filtres
Effacer les filtres

Assign different input using for loop

1 vue (au cours des 30 derniers jours)
Mark Ruben
Mark Ruben le 15 Fév 2017
Hi,
This is probably a simple question for most of you but I've been struggling with it for far too long. I have several lines of codes which will eventually get an output(O). When I only use a single input(in), the codes work fine but let's say I want to use a vector as an input and get an output with the same size as the input vector (each value in input vector will get corresponding output), how should I go about it? I've tried using for loop but no luck.
%%Loading variables
weight = unifrnd(-10, 10, [1 13]);
W1 = weight(1,1:4); % Input Layer Weight
W2 = weight(1,5:8); % Input Layer Bias
W3 = weight(1,9:12) % Hidden Layer Weight
W4 = weight(1,13); % Hidden Layer Bias
I.I = W1.*in % Input x weight
I.B = W2; % Bias
H.SUM = I.I+I.B; % Hidden Neuron
H = tansig (H.SUM) % Hidden Neuron Squashed
O.SUM = dot(H,W3') + W4 % Hidden Neuron x Weight + Bias
O = purelin (O.SUM) % Output Neuron

Réponses (1)

Rahul Kalampattel
Rahul Kalampattel le 15 Fév 2017
Modifié(e) : Rahul Kalampattel le 15 Fév 2017
I edited three lines (the commented out ones), the code now allows you to enter a row vector of inputs and returns a row vector of outputs. The variables I.I and I.B are now matrices; you could do achieve the same result with for loops, but I think it's faster this way.
%%Loading variables
weight = unifrnd(-10, 10, [1 13]);
W1 = weight(1,1:4); % Input Layer Weight
W2 = weight(1,5:8); % Input Layer Bias
W3 = weight(1,9:12); % Hidden Layer Weight
W4 = weight(1,13); % Hidden Layer Bias
%I.I = W1.*in
I.I = W1'*in % Input x weight
%I.B = W2
I.B = repmat(W2',1,length(in)) % Bias
H.SUM = I.I+I.B % Hidden Neuron
H = tansig (H.SUM) % Hidden Neuron Squashed
%O.SUM = dot(H,W3') + W4
O.SUM = W3*H + W4 % Hidden Neuron x Weight + Bias
O = purelin (O.SUM) % Output Neuron
  2 commentaires
Mark Ruben
Mark Ruben le 16 Fév 2017
You're awesome man! Also do you know how I import a column from Excel and then transpose it into a row vector. I tried to use the dataset function then transposing it using "'" but it's causing an error, "too many output argument".
Rahul Kalampattel
Rahul Kalampattel le 17 Fév 2017
Modifié(e) : Rahul Kalampattel le 17 Fév 2017
You can use xlsread to read data from Excel spreadsheets (see documentation), something like this:
data = xlsread('testSpreadsheet');
Once you've done that, you can transpose columns into rows with
data = data';

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by