subsetting a three dimensional array from elements in a vector

Hi,
I am new to Matlab and not an experienced programmer. I have a color photograph im1 (jpeg) and I want to sample the RGB values every 1cm across the image. I found that 34px is roughly 1cm on my own printer (I took the 'standard' of 37.78px per cm and experimented with my own equipment).
So for my whole image array (400 x 20 x 3) I would like to return the rgb values every 34th row. I set up a simple array:
b = 1:34:400
b = b'
This gives me a 12x1 vector which corresponds to the rows in im1 I would like to sample. But as stupid as it sounds I do not know how to do this. I tried setting up conditional statements with multiple conditions:
for i = 1:x %where x is number of rows in im1
if i == 1 | i == 34 | i == 68 | i == 102 | i == 136 | i == 170 | i == 204 | i == 238 | i == 272 | i == 306 | i == 340 | i == 374
newarray = im1(i,:,:);
end
end
Not only is this extremely unwieldy, it also doesn't work. This seems basic but I can't figure out what to do. There must be a better way -- can anyone help?

 Accepted Answer

It is actually very good for a first attempt! You might want to do something like the following actually:
b = 1:34:400 ;
newarray = im1(b,:,:) ;
When you are unsure about addressing, try with a simple array before increasing the complexity, e.g.
1D case:
>> v = randi(100, 1, 10)
v =
100 8 45 11 97 1 78 82 87 9
>> id = 1:3:10
id =
1 4 7 10
>> v(id)
ans =
100 11 78 9
2D case:
>> v = randi(100, 2, 10)
v =
40 81 92 27 14 58 15 63 52 8
26 44 19 15 87 55 86 36 41 24
>> v(:,id)
ans =
40 27 15 8
26 15 86 24
PS: for learning MATLAB, I usually recommend the official manuals that you can find there as PDFs: http://www.mathworks.com/help/pdf_doc/allpdf.html
I like them because, as far as I am concerned, they have the right proportion text/examples. I recommence especially the following documents in the MATLAB section: MATLAB Primer, Mathematics, and Programming Fundamentals.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Asked:

Kay
on 14 Jul 2013

Community Treasure Hunt

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

Start Hunting!