what does it mean by index exceed matrics dimension

%function for calculate the RMSE
function RMSE = rmse(data, k)
%here size of data and k must be same
%initially remove records with NaNs in data and k
dd = ~isnan(data) & ~isnan(k);
%for data
data = data(dd);
%for k
k = k(dd);
%compute RMSE
RMSE = sqrt(sum((data(:) - k(:)).^2) / numel(data));
end

2 Comments

In R2016b or later, you could get that error in the line
data = data(dd);
in the circumstance that the input data and k were not the same size, especially if one of them was a row and the other one was a column.
"what does it mean by index exceed matrics dimension"
Imagine that you define a vector with four elements in it:
M = [1,2,3,4];
And then you try to access the one millionth element:
M(1000000)
Does that vector have a one-millionth element? No, it does not. Trying to access an element that does not exist is an error. That is what MATLAB is telling you: your index exceeds the size of the array that you are indexing into.

Answers (2)

"index exceed matrix dimension" means that the index which you are trying of extract from matrix is out of the size of matrix. For example,
>> A=rand(4,6)
A =
0.1622 0.1656 0.6892 0.2290 0.5383 0.1067
0.7943 0.6020 0.7482 0.9133 0.9961 0.9619
0.3112 0.2630 0.4505 0.1524 0.0782 0.0046
0.5285 0.6541 0.0838 0.8258 0.4427 0.7749
The size of A is 4*6. Now, if you try to extract 2,7 element from A, then it will show the error that
>> A(2,7)
Index exceeds matrix dimensions.
As per @Walter Sir Apart from this, there is no error like Index exceeds matrix dimensions.
Regarding Index exceeds matrix dimensions @Ankur well explained
Be Ensure that data and k having the same size, it must be, a k is resultant from data
data original and k is the precessed, definitely, both have the same size.
I have tested the code an example, no error
function RMSE=rmse(data,k);
%here size of data and k must be same
%initially remove records with NaNs in data and k
dd=~isnan(data) & ~isnan(k);
%for data
data=data(dd);
%for k
k=k(dd);
%compute RMSE
RMSE=sqrt(sum((data(:)-k(:)).^2)/numel(data));
end
And called the function
data=rgb2gray(imread('test.png'));
k=imnoise(data,'gaussian');
result=rmse(data,k);

This question is closed.

Asked:

on 2 Oct 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!