how to find the area of a fraction of the matrix with the specific element
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
shiva
am 14 Okt. 2014
Kommentiert: shiva
am 14 Okt. 2014
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 Kommentare
Akzeptierte Antwort
Mohammad Abouali
am 14 Okt. 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 Kommentare
Mohammad Abouali
am 14 Okt. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!