I'm executing image scrambling code but im getting index exceeds matrix dimension error.

code i ws exceuting is:
iter=10;
[m,n]= size(W2);
out=zeros();
n=n-1;
for j=1:length(iter)
for y=0:n
for x=0:n
P=[1 1;1 2]*[x;y];
out(mod(P(2),m)+1,mod(P(1),m)+1)=W2(y+1,x+1);
end
end
W3=out;
end

Answers (1)

There are several issues with your code.
out = zeros()
creates an out matrix of size 1x1. Although it's not so critical as matlab will automatically (but slowly!) grow it in the loop, surely that's not the size you intended. Most likely you meant
out = zeros(size(W2));
The main problem with your code is that you're mixing up your m and your n. For a start, instead of using meaningless variable names such as m and n how about using useful names such as height and width:
[height, width] = size(W2);
Then of course, x goes from 0 to width, and y goes from 0 to height (instead of both to n == width as you've written). Similarly, the modulus across each dimension needs to be a different variable not m == height in both case.

Asked:

on 2 Jan 2018

Answered:

on 2 Jan 2018

Community Treasure Hunt

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

Start Hunting!