test values of matrix without loop to optimize my function

11 views (last 30 days)
Hi evbdy,
I would like to optimize my code, to test a values of matrix (without loop for) and store result in a vector to find index of nonzero element : (it's a code for image processing, image : 2160*1434)
for k=1:a %a=2160
for t = 1:b %b=1434
for l=1:numOfImages
if S(k,t,l)<Sombre
mat(l,1)=S(k,t,l);
end
end
F=find(mat);
if length(F)>=3
.......Continuation of code
Think you for help
  2 Comments
Michelangelo Ricciulli
Michelangelo Ricciulli on 14 Mar 2017
Is it correct that you overwrite many times the matrix mat during your for loop? I don't get the rationale... Maybe you want the matrix mat to be different from zero only when the l-th image satisfies at least once that S(k,t,l)<Sombre. Am I correct?
telkab01
telkab01 on 14 Mar 2017
Yes i overwite vector mat during my loop for, i used it uniquely to caluclate a combination matrix to calculate a normal surface for each illumination. (photometric stereo problem). I want to test a 3D matrix S(k,t,1:6) and get the index for values wich respect the condition (Sombre<90), to use the vector of index later.. it's like a binary test but i dont see how i can do it with a fast maneer.. thank you

Sign in to comment.

Accepted Answer

Jan
Jan on 14 Mar 2017
Perhaps you want something like this:
for k = 1:a
for t = 1:b
M = S(k, t, :);
if sum(S(:) < Sombre) >= 3
...
Actually I assume that a vectorization might be useful:
S2 = (sum(S < Sombre, 3) >= 3);
Now S2 contains a TRUE on all positions, which match the criterion.
  1 Comment
telkab01
telkab01 on 14 Mar 2017
Thank you for answer, for each k,t (pixels) i have 6 differents values and i want to test them and return a vector with true or false for each position to use it later in my code. then i can't test all of values of S in one time beacause it's not useful for my case.

Sign in to comment.

More Answers (1)

John BG
John BG on 14 Mar 2017
Hi Telkab01
a=2160; b=1434; % image size
ni=10 % number of images
S % matrix containing the images with format S(a(i),b(j),ni(k))
no need for any loop, look:
S2=S(:)
v=find(S2==0)
[a0,b0,ni0]=ind2sub(size(S2),v)
a0 b0 are vector containing the coordinates of the zeros in each image, and ni0 contains the image numeral where zeros are located.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  1 Comment
telkab01
telkab01 on 14 Mar 2017
Thank you for your answer, I want to test a 3D matrix S(k,t,1:6) and get the index for values wich respect the condition (Sombre<90), to use the vector of index later.. it's like a binary test but i dont see how i can do it with a fast maneer.. S It's a matrix values and not a image, and for each pixels (k,t) i do my test. thank you

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!