calling a function for a number of indexes in a matrix
2 views (last 30 days)
Show older comments
Hi,
I have an image [N M], and a matrix of [N-r M-c]. I want to call a certain function on the indexes where image(i,j) < threshold
I'm new to MATLAB so my initial approach was
for i = 1 : height
for j = 1 : width
if (image(i, j) < thresh)
f([i j height width], c);
c = mod(c, 8) + 1;
end
end
end
is there an easier way to do it automatically without looping over the matrix? (having 'c' loop on values 1 to 8 for each call to the function?)
2 Comments
Answers (1)
Jakob Sørensen
on 3 Nov 2012
Can't you just get a logical of where the matrix is above/below the thresh? And then multiply it with your matrix. And then do the function on that matrix. Like this:
logicals = image < thresh;
image_new = image.*logcals;
f(image, c);
This might need some adjustment, depending on you function. If it contains plus/minus, this probably won't work.
See Also
Categories
Find more on Matrix Indexing 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!