test values of matrix without loop to optimize my function
13 ビュー (過去 30 日間)
古いコメントを表示
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?
採用された回答
Jan
2017 年 3 月 14 日
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 件)
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
参考
カテゴリ
Help Center および 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!