how to find the area of a fraction of the matrix with the specific element
2 views (last 30 days)
Show older comments
Hello,
I have a code that changes the element of zero matrix to one in time. I want to find a way to calculate the area of a fraction of the square domain ( square matrix) that contains the elements of 1 at any specific time. my code is as follows. thanks in advance
clear all;
m=200;
n=200;
G=zeros(m,n);
G(100,1)=1;
A(1,1)=1;
for i=2:1:500
sol=roots([1 -A(i-1,1) -1]);
A(i,1)=sol(sol>=0);
end
for t=1:1:n
G(100,t)=1;
for i=1:1:t
for j=1:1:A(t-i+1)
G(100+j,i)=1;
G(100-j,i)=1;
end
end
end
for t=1:1:200
for k=100:-1:1
if A(t+200-k,1)<100
for j=1:1:A(t+200-k,1)
G(100+j,k)=1;
G(100-j,k)=1;
end
end
end
end
0 Comments
Accepted Answer
Mohammad Abouali
on 14 Oct 2014
Just do this: assuming that A is the matrix that you have its element changing to 1 and you want to see what fraction of this matrix are 1
mask= (A==1);
fractionAreaBeingOne= sum(mask(:))./numel(A);
3 Comments
Mohammad Abouali
on 14 Oct 2014
Actually if your matrix A is only storing zeros and ones then you can even reduce it too:
fractionAreaBeingOne= sum(A(:))./numel(A);
But this works only if you store nothing but 0 and 1 in A.
More Answers (0)
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!