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

1 visualización (últimos 30 días)
O Mueller
O Mueller el 27 de Oct. de 2017
Editada: Cedric el 27 de Oct. de 2017
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
r1= 10; %radius sphere
r2=10; %radius disk
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);

Respuesta aceptada

Cedric
Cedric el 27 de Oct. de 2017
>> sum(~imag(za(:)))
ans =
8627
  2 comentarios
O Mueller
O Mueller el 27 de Oct. de 2017
like this it works kind of.
z=[za zb];
M= sum(imag(z(:))~=0);
Cedric
Cedric el 27 de Oct. de 2017
Editada: Cedric el 27 de Oct. de 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(:))) ;

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by