## How do i count a certain class of numbers in a 100x100 matrix?

### O Mueller (view profile)

on 27 Oct 2017
Latest activity Edited by Cedric Wannaz

on 27 Oct 2017

### Cedric Wannaz (view profile)

I wrote this code and i want to count the number of non imaginary numbers in the matrices za and zb. is there a way to count through the elements of a matrix? or do i now have to use a for loop?
I had to do a vaktorization already, because with a for loop it took 0.4 sec per iteration. so if you find any error please tell me about it also.
%define simulation variables
N=10000; %<--- PLEASE SET NUMBER OF RAYS <-----
M=0; %numberhits
%define Geometry
h=20; %height sphere center
%Analytic solution viewfactor disk sphere
F21ana= 2*(1-1/(sqrt(1+(r2/h)^2)))*(r1/r2)^2
%determines the viewfactor disk to sphere
n=[0;0;-1];
t1=[1;0;0];
t2=[0;1;0];
R1= rand(sqrt(N));
R2= rand(sqrt(N));
r=r2*sqrt(R1);
phi=2*pi()*R2;
%create the coordinates of the point on disk
x2=r*cos(phi);
y2=r*sin(phi);
z2=h;
R1= rand(sqrt(N));
R2= rand(sqrt(N));
thet=sin(sqrt(R1))^-1;
phi=2*pi()*R2;
%create the direction vector of the ray
ux=sin(thet)*cos(phi);
uy=sin(thet)*sin(phi);
uz=-1*cos(thet);
%define variables for equation system
A=1+(uy./uz).^2 + (ux./uz).^2;
B=-2*z2*( (uy./uz).^2 + (ux./uz).^2) + 2*y2*uy./uz +2*x2*ux./uz;
C=y2^2 + x2^2 - r1^2 +z2^2*( (uy./uz).^2 + (ux./uz).^2)-z2*( 2*y2*uy./uz +2*x2*ux./uz);
%solve system
za= (-B + sqrt(B.^2 - 4*A.*C))./(2*A);
zb= (-B + sqrt(B.^2 + 4*A.*C))./(2*A);

### Cedric Wannaz (view profile)

on 27 Oct 2017

>> sum(~imag(za(:)))
ans =
8627

O Mueller

### O Mueller (view profile)

on 27 Oct 2017
like this it works kind of.
z=[za zb];
M= sum(imag(z(:))~=0);
Cedric Wannaz

### Cedric Wannaz (view profile)

on 27 Oct 2017
Just do it on both arrays:
>> n_za = sum(~imag(za(:))) ;
>> n_zb = sum(~imag(zb(:))) ;
or
>> n_tot = sum(~imag(za(:))) + sum(~imag(zb(:))) ;