SUNY Geneseo Department of Mathematics
Monday, April 6
Math 230 02
Spring 2015
Prof. Doug Baldwin
for
loops driven by length or size of vector
with functions or expressions that operate on each element internallyfor i = 1 : length(v)
w( i ) = f( v(i) )
end
w = f( v )
>> mars = imread( 'Chromakey/MarsLookoutPoint.jpg' );
>> size(mars)
ans =
387 516 3
>> mars( 1, 1, 1 )
ans =
178
>> mars( 1, 1, 2 )
ans =
135
>> imshow( mars )
function [ b ] = brightest( img )
%BRIGHTEST Find the highest color intensity in an image
b = 0;
[maxR, maxC, maxP] = size( img );
for r = 1 : maxR
for c = 1 : maxC
for p = 1 : maxP
if img(r,c,p) > b
b = img(r,c,p);
end
end
end
end
end
>> brightest(mars)
ans =
198
>> max(mars)
ans(:,:,1) =
Columns 1 through 15
178 179 178 179 180 180 180 178 179 177 178 178 178 178 184
...
ans(:,:,2) =
Columns 1 through 15
137 137 136 135 136 138 137 137 135 135 136 136 136 134 134
...
ans(:,:,3) =
Columns 1 through 15
96 95 94 96 97 96 95 96 96 95 96 96 96 95 95
>> size(max(mars))
ans =
1 516 3
>> max(max(mars))
ans(:,:,1) =
198
ans(:,:,2) =
138
ans(:,:,3) =
97
>> max(max(max(mars)))
ans =
198
>> tic
>> toc
Elapsed time is 4.999782 seconds.
>> tic;brightest(mars);toc
Elapsed time is 1.618765 seconds.
>> tic;max(max(max(mars)));toc
Elapsed time is 0.000567 seconds.