how can I use my function on 3*3 blocks?

1 回表示 (過去 30 日間)
jack nn
jack nn 2015 年 4 月 9 日
コメント済み: Mohammad Abouali 2015 年 4 月 10 日
hi I have a matrix ,I wanna use a function on 3*3 blocks. my function is: if in a block we watch just a one this block should return 1 but if all 9 elements of this block is zero this function should return 0 how can I do this in matlab?
...all the elements of this matrix is zero and one.
  1 件のコメント
Image Analyst
Image Analyst 2015 年 4 月 9 日
Give examples of the 3x3 blocks that return 1 and 0. I especially don't understand the grammar of "if in a block we watch just a one this block should return 1". Exactly what returns a 1? Does this:
[.2 .3 .4
.3. .5 .6
.6 .7 .9]
Does this?
[0 0 1
0 0 0
0 0 0]

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

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 4 月 10 日
編集済み: Mohammad Abouali 2015 年 4 月 10 日
inputData=zeros(10,10);
inputData(randi(100,[15,1]))=1
inputData =
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0
% this is what you are looking for
result=blockproc(inputData,[3,3],@(block) any(block.data(:)~=0))
result =
0 0 1 0
1 0 1 1
1 0 1 0
1 1 0 0
% and if you want both inputData and result matrix be the same size do this:
result=blockproc(inputData,[3,3],@(block) any(block.data(:)~=0)*ones(3,3))
result =
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
  2 件のコメント
jack nn
jack nn 2015 年 4 月 10 日
dear Mohammad Abouali ,I really appreciate you for your help.
Mohammad Abouali
Mohammad Abouali 2015 年 4 月 10 日
you are welcome

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

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2015 年 4 月 10 日
m = M(a:a+2,b:b+2); % This is your "block"
t = +any(m(:)~=0); % This is the returned value

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by