Info

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

Index error during encryption

2 views (last 30 days)
Braden Sasdelli
Braden Sasdelli on 12 Nov 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
im encrypting a message in an image and im having trouble getting the message to go through the cypher. i get an error at line 8 that says "Index exceeds the number of array elements (22)." and cant figure out how to fix it.
l = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '];
n = 1:27;
m = 'this is a test message';
d = 1;
em = zeros(1,length(m));
for i = 1:length(m)
for ele = 1:length(l)
if m(d)==l(ele)
em(d)=n(ele);
d=d+1;
end
end
  1 Comment
Adam
Adam on 12 Nov 2019
Tick the 'Pause on errors' option in the run menu then just look at d in the workspace or command window when it stops. Apparently it is > 22.

Answers (1)

Stijn Haenen
Stijn Haenen on 12 Nov 2019
You can use this script:
l = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '];
n = 1:27;
m = 'this is a test message';
d = 1;
em = zeros(1,length(m));
for i = 1:length(m)
em(i)=find(l==m(i));
end

Community Treasure Hunt

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

Start Hunting!