Matrix manipulation syntax help

Hey, I have a 2048x2048 matrix with values of either 0 or 1 which is called "white". I need to find the total number of 3x3 blocks in the matrix where each data point has a value of 1, ie,
1 1 1
1 1 1
1 1 1
Im a first time user and I think that my problem is syntax. My code is as follows,
x=0
for i=2:2047 j=2:2047
if white(i-1,j-1)==1 & white(i-1,j)==1 & white(i-1,j+1)==1 & white(i,j-1)==1 & white(i,j)==1 & white(i,j+1)==1 & white(i+1,j-1)==1 & white(i+1,j)==1 & white(i+1,j+1)==1
x=x+1
end
y=sum(x)
end
Ive been at it for a few hours and any help would be really appreciated. Ill be spending the rest of the afternoon trying to solve this problem....
Thanks for your help!

1 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日
Welcome to MATLAB answers. Your post is well written, you provide: an example, what you've tried, and what you want - everything we typically request.

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

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日

0 投票

You could do this with your for-loop like this:
n = 0;
for ii = 2:2047
for jj = 2:2047
if(all(all(white(ii-1:ii+1,jj-1:jj+1))))
n = n+1;
end
end
end

2 件のコメント

Andy
Andy 2011 年 5 月 10 日
Derek, also take note of Sean's index variables: ii and jj. If you use i and j as index variables, you overwrite the complex unit i = j = sqrt(-1). Instead, it is common to use ii, jj, ix, and jx as indexing variables for loops, so as to leave i and j for complex computations.
Derek
Derek 2011 年 5 月 11 日
Thanks for your help! I had to make some changes to my original code, but your answer served as a great template for me. I just started a summer job where I will be doing lots of Matlab. This forum is great, thanks again.

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

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日

0 投票

This will probably frustrate you, but here's a one-liner to do exactly what you want!
n = sum(sum(conv2(double(white),ones(3),'valid')==9));
Algorithm:
  • convolve the "white" matrix with a kernel of ones the size you want. Only valid applications apply.
  • It will only equal 9 when all of the values in white matching the kernel are == 1 (i.e. (1*1)*9).
  • sum it twice to get the total number.
  • Enjoy a coffee.

6 件のコメント

Matt Tearle
Matt Tearle 2011 年 5 月 10 日
Of course you actually meant
n = nnz(conv2(double(white),ones(3),'valid')==9);
:)
Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日
Eww. No. nnz is super slow, avoid it like the plague:
t1 = 0;
t2 = 0;
white = rand(2048)>.4;
for ii = 1:10
tic
n1 = sum(sum(conv2(double(white),ones(3),'valid')==9));
t1 = t1+toc;
tic
n2 = nnz(conv2(double(white),ones(3),'valid')==9);
t2 = t2+toc;
end
disp([t1 t2])
Matt Fig
Matt Fig 2011 年 5 月 10 日
It's a shame too. Because the NNZ solution looks so much cleaner and is easier to read.
Walter Roberson
Walter Roberson 2011 年 5 月 10 日
I thought nnz() was the preferred form?
On my system, nnz() is only about 2% slower in the above test. And nnz() would be considerably faster than double-sum on sparse arrays.
Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日
That's why I keep a handy little one line function around vec
function x = vec(x)
x = reshape(x,numel(x),1);
Then sum(vec(...)) %At least no double sum because those are clumsy
Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日
Aren't sparse arrays its main (only for my purposes) use?

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

Matt Fig
Matt Fig 2011 年 5 月 10 日

0 投票

Another suggestion:
x2 = length(findsubmat(white,ones(3)));
Also, as far as your code, you were close. This works:
x = 0;
for ii = 2:2047
for jj = 2:2047
if white(ii-1,jj-1) & white(ii-1,jj) &...
white(ii-1,jj+1) & white(ii,jj-1) &...
white(ii,jj) & white(ii,jj+1) &...
white(ii+1,jj-1) & white(ii+1,jj) &...
white(ii+1,jj+1) %#ok
x = x+1;
end
end
end
Andrei Bobrov
Andrei Bobrov 2011 年 5 月 10 日

0 投票

more
mn = size(white)-2;
C = arrayfun(@(k)cumsum([1:3;ones(k,3)]),mn,'un',0);
C2 = arrayfun(@(s)cumsum(ones(mn),s),[1 2],'un',0);
x = nnz(arrayfun(@(i,j)all(all(white(C{1}(i,:),C{2}(j,:)))),C2{:}));

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by