필터 지우기
필터 지우기

Assign different input using for loop

조회 수: 1 (최근 30일)
Mark Ruben
Mark Ruben 2017년 2월 15일
편집: Rahul Kalampattel 2017년 2월 17일
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

답변 (1개)

Rahul Kalampattel
Rahul Kalampattel 2017년 2월 15일
편집: Rahul Kalampattel 2017년 2월 15일
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
Mark Ruben
Mark Ruben 2017년 2월 16일
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 2017년 2월 17일
편집: Rahul Kalampattel 2017년 2월 17일
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';

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by