How to fix: Index Exceeds Matrix Dimensions

Hello,I am doing an anomaly detection algorithm on the matlab. My code needs to read the data row by row in order to execute the code to meet the requirement. But, the time I execute the code, I encounter the Index Exceeds Matrix Dimensions error and I do not know how to solve the error. Hereby, I attached the program code. S1 is a matrix with the size 300*8, w with size 1*8 and h with size 1*1. The matrix w and h is updated on every iteration.
clc
index=0;
load ('S1.mat')
load ('var.dat','-mat');
learning_rate=0.1;
data_size=size(S1);
for i=1:data_size(1)%%indicating size of matrix S1 row
index=index+1;
current_reading=S1(i,:);
neuron_matrix_size=size(w);
for j=1:neuron_matrix_size(1)%%indicating size of matrix w row
sum_reading=0;
for k=1:neuron_matrix_size(2)%%indicating size of matrix w column
square_distance=(current_reading(k).^2-w(j,k).^2);
sum_reading=sum_reading+square_distance;
end
euc_distance_neuronj=sqrt(sum_reading);
if j==1
euc_distance = [euc_distance_neuronj];%%this is the first time create this matrix
else
euc_distance = [euc_distance euc_distance_neuronj];%%this is when the matrix has been created
end
end
%%find the best matching neuron and determine which neuron win
[minimum_distance,winning_neuron] = min(euc_distance);
%%disp(euc_distance,'euc_distance')
%%disp(minimum_distance,'minimum_distance = ')
if minimum_distance<0.3
h(winning_neuron)=h(winning_neuron)+1;
%%update weight
for k=1:neuron_matrix_size(2)
w(k,winning_neuron)=w(k,winning_neuron)+learning_rate*(current_reading(k)-w(j,k));
end
else
%%disp(w,sensor_reading)
h = [h 1];
w = [w;current_reading];
end
end
Is it any machine learning toolbox that I can use as anomaly detection for the result comparison?

3 Comments

If you don't tell us which line the error is on it is hopeless for someone to just look through the code and try to work it out! Help us to help you by giving complete information.
Sorry for the incomplete information. The error encounter on line 32 which use for updating the weight:
for k=1:neuron_matrix_size(2)
w(k,winning_neuron)=w(k,winning_neuron)+learning_rate*(current_reading(k)-w(j,k));
end
You have this line:
neuron_matrix_size=size(w);
and then:
for k=1:neuron_matrix_size(2)
w(k,winning_neuron)=w(k,winning_neuron)+learning_rate*(current_reading(k)-w(j,k));
end
further down. Since you are indexing iinto the first dimension of w using k you should be using
neuron_matrix_size(1)
in your for loop instead.

Answers (1)

for k=1:neuron_matrix_size(2)
w(k,winning_neuron)=w(k,winning_neuron)+learning_rate.*(current_reading(k)-w(j,k));
end
try .* instead of *

This question is closed.

Asked:

on 11 Apr 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!