Info

This question is closed. Reopen it to edit or answer.

how to rectify this error and make the code work?

1 view (last 30 days)
Anushree Bhatt
Anushree Bhatt on 7 May 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
i am facing problem in the decryption code , however encryption code is working correctly. the error coming is : ??? Index exceeds matrix dimensions.
Error in ==> Decrypt_text at 15 while(im(x,y)~=0)&(x<q)&(y<p)

Answers (2)

Joseph Cheng
Joseph Cheng on 7 May 2015
for the error "index exceeds matrix dimensions" it is far simpler for you to debug it and learn from it. All you need to do is just put a debug breakpoint at that spot or a try-catch debug code to catch the error at which iteration.
Without digging all your code I can venture a guess just by looking at the error line. I am pretty certain you're incrementing x and y in the loop. And at some point you're doing x= x+1 or y = y+1 one too many times such that it no longer within the dimensions of im(). So to correct this your while conditional statement should be adjusted or pick when you're incrementing x or y such that you don't exceed dimensions of im().

Geoff Hayes
Geoff Hayes on 7 May 2015
Anushree - since your p and q are determined by
[p,q,r]=size(I'm);
and your conditions for the while loop are
im(x,y)~=0&(x<q)&(y<p)
change the order of the conditions so that you check whether x and y are less than the number of rows and columns of im before you try to use them as indices into im. Try using
while x<=q && y<=p && im(x,y)~=0
Note that I've changed the conditions to allow for equality (maybe this isn't correct for your tests but they are still valid indices for im) and that I've replaced the & with && because it exhibits the short circuiting behaviour that the single ampersand does not.
  2 Comments
Anushree Bhatt
Anushree Bhatt on 8 May 2015
now there is no error but the code is not giving the desired output. i had entered the string 'hello' , on decryption i am supposed to get the value of 'hello' back at str . Instead i am getting a very weird value :
T,V.$.ze: N6MB`2F,
this is the value.
Anushree Bhatt
Anushree Bhatt on 8 May 2015
i tried a different thing .I changed the contents of the entire while loop. the following attached is the new code for decryption but it is not giving the desired output.

This question is closed.

Community Treasure Hunt

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

Start Hunting!