test values of matrix without loop to optimize my function
이전 댓글 표시
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
Michelangelo Ricciulli
2017년 3월 14일
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
2017년 3월 14일
채택된 답변
추가 답변 (1개)
John BG
2017년 3월 14일
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
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!