I AM GETTING INDEX EXCEEDS MATRIX DIMENSIONS error? PLEASE HELP?

The following code is used for digital image watermarking using the feature points of the cover image I am getting the following error: Index exceeds matrix dimensions. Error in Algo1 (line 115) M(i1,j1)=a1(i1,j1); Please tell me how to resolve this error code (m file attached for reference)

6 commentaires

@Aniket Paranjpe: That code is very badly aligned and indented. Badly aligned code is one way that beginners hide code errors. You should use the MATLAB editor's default alignment settings. You can also align the code in the editor by selecting all text and then clicking ctrl+i.
You also need to write comments and clearly explain what the code does.
I Have added a few comments about what the code does, also i have indented the code properly. please go through updated m-file and help me resolve this error
The failing line
Error in Algo1 (line 115) M(i1,j1)=a1(i1,j1)
does not occur in the posted code. So how could we fix a problem in the code, if you post a different one?
i have only added comments in the code, code is same as previous. please help me resolve the error @Jan Simon
Unfortunately we do not have lena.bmp or watermark.png to test with.
I Have attached the test images now too.

Connectez-vous pour commenter.

 Réponse acceptée

Near line 120, you have
i=1:floor(l/l1);
At that point, l is 64 and l1 is 512, so floor(l/l1) is 0 and i = 1:0 which is empty. Then a small number of lines further down,
for i1=1:l1
for j1=1:m1
temp=i1+(i-1)*l1;
temp1=j1+(j-1)*m1;
M(i1,j1)=a1(temp,temp1);
end
end
with i being empty, temp becomes empty, and a1 gets indexed with the empty vector, giving an empty result. The empty result cannot be stored in the non-empty M(i1,j1)
Now suppose that you had
i=1:floor(l1/l);
giving i=1:512/64 which is i=1:8 . Then with i being a vector, temp=i1+(i-1)*l1 would be a vector, and a1(temp,temp1) would be addressing multiple locations. You cannot store multiple locations in the single location M(i1,j1) either. Therefore that loop is probably wrong.

4 commentaires

@walterRoberson I made the change that you suggested (at line 120).now I am getting temp and temp1 as 1x8 non zero vectors.Now can u tell me how to store temp and temp1 for further processing?. after making change at (line 120) i am getting temp and temp1 as: temp =
1 4097 8193 12289 16385 20481 24577 28673
Change
M=[ra1, ca1];
to
M = cell(l1, m1);
Change
M(i1,j1)=a1(temp,temp1);
to
M{i1,j1}=a1(temp,temp1);
Change
newmatrix(temp,temp1)= M(i2,j2);
to
newmatrix(temp,temp1)= M{i2,j2};
However, I do not at the moment see why you bother to take chunks of a1 and put them into M and then take the chunks and put them into newmatrix. At the moment it appears that all of that work could be done by just
newmatrix = a1;
Perhaps I missed something.
You must have made a typing mistake in the conversion.
Your code was also missing an opening quote mark for an imwrite, and it used the undefined function imscale(). If imscale was intended to be https://www.mathworks.com/matlabcentral/fileexchange/21251-geoml?focused=5104132&tab=function then it was being called incorrectly.
I have attached code that runs.
@ walter roberson this piece of code works,thankyou

Connectez-vous pour commenter.

Plus de réponses (2)

Start with an auto-indentation to make the code readable: Ctrl-A Ctrl-I. Omit the brute clearing header "clc; close all; clear all;" - there is no reason to clear e.g. all loaded function from the RAM and waste time with reloading from the slow disk.
I cannot run your code due to missing input data and toolboxes.
The code looks strange:
for i1=1:l1
for j1=1:m1
temp=i1+(i-1)*l1;
temp1=j1+(j-1)*m1;
M(i1,j1)=a1(i1,j1);
end
end
What is the reason for overwriting temp and temp1 in each iteration? This would be much simpler:
M = a(1:l1, 1:m1);
But you should get the same error: Obviously a has either less than l1 rows or less than m1 columns. You can find out the details using the debugger:
dbstop if error
Then run the code again. When it stops, check the current values of i1 and j1 as well as the dimension of a1.
The missing comments impede the clarity of the code massively. I would not dare to debug this code or use it.

1 commentaire

I Have added a few comments about what the code does, also i have indented the code properly. please go through updated m-file and help me resolve this error

Connectez-vous pour commenter.

Aniket Paranjpe
Aniket Paranjpe le 27 Nov 2017
Modifié(e) : Walter Roberson le 27 Nov 2017
@Walter Roberson After making the changes you suggested i am getting index exceeds matrix dimensions error at Error in a12 (line 99)
M{i1,j1}=a1(temp,temp1);

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by