how to find the area of a fraction of the matrix with the specific element

2 views (last 30 days)
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

Accepted Answer

Mohammad Abouali
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
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.
shiva
shiva on 14 Oct 2014
thanks for getting back to me. I will use your second suggestion since all the elements are one or zero. I just have one other question. the problem that I'm dealing with is a square domain that is zero everywhere in the begininng. then it startes to change to one as time passes. actually it uses the elements of matrix A. for instance, at the second time step the height from the center line that changes to one is the second element of matrixA and so on. if you take a look at my foor loops, you can understand the logic of how this change grows in the y direction from the centerline. the important point here is that the A matrix elemetn are not integers. I was wondering whether there is any other way of chosing the domain. this approach is not precise since I'm using a matrix and it rounds all the elements to the nearest integer number. I hope i have not been confusing and I appreciate all your valuable suggestions in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!