calling a function for a number of indexes in a matrix

조회 수: 4 (최근 30일)
Greg
Greg 2012년 11월 3일
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
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 3일
what is
f([i, j, height, width], c);
Greg
Greg 2012년 11월 3일
편집: Greg 2012년 11월 3일
my bad that was supposed to be
f([i j height width], c)
f is the function i need to call, it takes two parameters, the first is a vector of the coords from the image (x,y) and the height and width of a specific pattern which don't not change in this loop.
c is the second parameter i send to the function, it is a natural number in the range [1,8] should increment by 1 upon every call to the function and reset to 1 after reaching 8

댓글을 달려면 로그인하십시오.

답변 (1개)

Jakob Sørensen
Jakob Sørensen 2012년 11월 3일
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.
  댓글 수: 1
Greg
Greg 2012년 11월 3일
편집: Greg 2012년 11월 3일
the function f doesn't care about the value within the image_new, it only needs the x,y of the indexes so i can simply apply
logicals = image < thresh;
however 'f' is supposed to take the following parameters
[image_new_x image_new_y height width], c
where height and width are constants and c alters between 1 and 8, how can i do that?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by