figure an matrix by image

Hellow, i have reconstruct an image of 16x16 pixels and all the pixels are in a vector of 256 elements P(256) , is it necessary to change P to a dimension P(16,16) to show it in an image. I thought it would but i tried it and it gives a error. Hope for a positive answer :)

 Accepted Answer

image(reshape(P,16,16))
What error are you getting?

10 Comments

I tried this and the error is:
figure
image(reshape(X,16,16))
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
Is it always necessary to reshape, because all the images i produce in my ART code have the form: I(x,1) where x is mostly 256,512 or 1024 and maybe you know there is a standard solution?
So show the output of this, placed right before the line that errors:
size(X)
If you have an array which is 512 or 1024, then that contradicts your initial claim that "all the pixels are in a vector of 256 elements P(256)"
If you _do_ have 512 or 1024 elements, then what outcome do you want? Still a 16 x 16 pixel outcome? If so then how do you want multiple input elements to be mapped in to the 16 x 16 output ?
No, in my program the user can choice how many pixels. And i must visualize the result. So if they select a 1024 image, i must image the vector with 1024 elements (thats a 32x32 pixel image). So i though that i must make it 32x32 to use it in image. Because image(vector) doesn't do anything...
Also when the user choice a 256(16x16) image, i also have to image it... so i need it in general.
I hope you understand it now :)
But already thank you very much!
L = sqrt(length(P));
if floor(L)~=L
error('Bad vector length.')
else
image(reshape(P,L,L))
end
The only thing I might do differently than Matt's answer is to use numel(P) instead of length(P)
@Walter, just out of curiosity why choose NUMEL over LENGTH?
Are you still suspicious that Fiboehh hasn't supplied a vector?
numel() covers the case where a non-vector was supplied but length() does not. numel() is as easy to write and covers more cases, so one might as well use it unless one explicitly wishes the code to error out in the case where a non-vector was supplied (and if so then why not test for that condition to make it obvious that one did not simply "overlook" that possibility ?)
That and I don't want to have another go around with someone saying, "But that failed for me" only to have it turn out that they had supplied a non-vector.
Exactly as I thought... Good enough!
Note that 512 does not have an integer square root. Fiboehh, what do you want to have happen if 512 is selected?

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!