test values of matrix without loop to optimize my function
11 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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.
More Answers (1)
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
See Also
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!