How can I convert 1D array into 2D array

Hello All, This is my sample code:
img=imread('C:\Users\nitin\Documents\MATLAB\t8.png');
img=rgb2gray(img);
[i,j]=size(img);
[x,y]=find(img);
for p=1:i*i
x1(p)=mod(32+(19*p),i);
end
for q=1:j*j
y1(q)=mod(16+(13*q),j);
end
x1=x1';
y1=y1';
After executing the above piece of code, I got x1(64*1) 1D array and y1(64*1) 1D array as new coordinates. Now I want to convert x1 and y1 into (8*8) matrix with new coordinates i.e. (x1,y1). Please help.

3 Comments

Please apply a proper code fiormatting: Instructions
What is it you want to do? x and y both contain 64 values, so you can convert both into 8x8 matrices. So how do you want to combine these two into one?
For changing the shape of a vector/matrix you can use the function reshape
Yes, I used the function "reshape", still no luck.

Sign in to comment.

 Accepted Answer

There are a few things that are not quite right with what you're doing
1) you're using x and y as row, column respectively. Standard convention is that x is the horizontal coordinate (column) and y is the vertical coordinate (row).
2) the x1 and y1 you calculate go from 0 to height or width minus 1. matlab indexing starts at 1, not 0. You need to add 1 to your coordinates.
Anyway, you wouldn't need reshape if you'd kept your x, y, x1, and y1 the same shape as the image. An added bonus is that you wouldn't even need the loop:
[y, x] = ndgrid(1:size(img, 1), 1:size(img, 2)); %order is [y, x] or [row, column]
x1 = mod(32 + 19*x, size(img, 2));
y1 = mod(16 + 13*y, size(img, 1));
scrambledimage = img(sub2ind(size(img), y1+1, x1+1));

4 Comments

Sorry for late response.Thank you so much for this solution. it really works as I wanted. Could you please explain me the importance of the given line:
[y, x] = ndgrid(1:size(img, 1), 1:size(img, 2));
I'm not sure what you mean by importance. It's the simplest way to get the coordinates of all the pixels in one go.
Ok, Thanks a lot Guillaume.
What if the case of extracting 2d from 1d for loop..??

Sign in to comment.

More Answers (1)

[i,j] = size(img);
x1 = mod(32 + 19* (1:i*i), i);
x1 = reshape(x1, 8, 8);
y1 = mod(16 + 13* (1:j*j), j);
y1 = reshape(y1, 8, 8);
The detail "i.e. (x1,y1)" is not clear to me.

4 Comments

Like (x,y) represents a coordinate of a pixel in an image, similarly, I am applying arithmetic operations on original coordinates and getting new coordinates (scrambling of pixels in using coordinates). In the method, I am calculating x1 and y1 separately and then have to combine them to get a final scrambled image. After getting new coordinates i.e. x1 and y1, I did-
ind = sub2ind(size(a), x1, y1);
pix = a(ind);
pix=reshape(pix,8,8);
But output is not correct. is is something like this:
185 185 185 185 185 185 185 185
37 37 37 37 37 37 37 37
114 114 114 114 114 114 114 114
161 161 161 161 161 161 161 161
15 15 15 15 15 15 15 15
185 185 185 185 185 185 185 185
79 79 79 79 79 79 79 79
143 143 143 143 143 143 143 143
It is repeating first expected pixel value to all 8 places in a row, similarly for other 7 rows. Whereas it is expected the pixel value corresponding to new coordinate. How would I get distinct pixel value in the output matrix?
Jan Simon answer doesn't work
Exactly, my answer does not work. I've posted it with the intention to clarify the question. See my:
The detail "i.e. (x1,y1)" is not clear to me.
I'm still not sure, what you, Shafali, are asking for, and hope, that Guillaume's answer helps you. If so, please accept his answer, or try to explain the problem with a small example of the input and the wanted result.
Done Sir. Guillaume's answer does, what I really wanted.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!