フィルターのクリア

How to minimize the loop function?

2 ビュー (過去 30 日間)
siti
siti 2014 年 10 月 31 日
編集済み: Mohammad Abouali 2014 年 10 月 31 日
hello,,i need to simplify this coding,maybe by minimizing only one loop..i really don't know how to do it..please help me asap.
function S = IraHistFunction (im)
I = imread ('lena.jpg'); [m,n] = size (I); S = zeros (256, 1);
for GrayValue = 0 : 255
for i = 1 : m
for j = 1 : n
if I(i,j) == GrayValue
S(GrayValue+1) = S(GrayValue+1) + 1 ;
end
end
end
end
figure, bar(S)

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 31 日
編集済み: Mohammad Abouali 2014 年 10 月 31 日
Change
for GrayValue = 0 : 255
for i = 1 : m
for j = 1 : n
if I(i,j) == GrayValue
S(GrayValue+1) = S(GrayValue+1) + 1 ;
end
end
end
end
to a single line as follow:
n=hist(I(:),0:255);
You can then plot it as follow:
bar(0:255,n);
  2 件のコメント
siti
siti 2014 年 10 月 31 日
ohh..thanks a lot..but can i minimize looping without using hist function?
Mohammad Abouali
Mohammad Abouali 2014 年 10 月 31 日
編集済み: Mohammad Abouali 2014 年 10 月 31 日
If you insist on not using * hist * you can do it this way:
change this:
for GrayValue = 0 : 255
for i = 1 : m
for j = 1 : n
if I(i,j) == GrayValue
S(GrayValue+1) = S(GrayValue+1) + 1 ;
end
end
end
end
to a code with only one loop as
for GrayValue = 0 : 255
S(GrayValue+1)=sum(I(:)==GrayValue);
end
or even shorter without any loop as
S=arrayfun(@(x) (sum(I(:)==x)), 0:255);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by