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

1 次查看(过去 30 天)
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);

采纳的回答

Cedric
Cedric 2017-10-27
>> sum(~imag(za(:)))
ans =
8627
  2 个评论
Cedric
Cedric 2017-10-27
编辑:Cedric 2017-10-27
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(:))) ;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by