how to find the area of a fraction of the matrix with the specific element
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Mohammad Abouali
2014년 10월 14일
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
Mohammad Abouali
2014년 10월 14일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!