how to find the area of a fraction of the matrix with the specific element
2 次查看(过去 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!