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

2 ビュー (過去 30 日間)
O Mueller
O Mueller 2017 年 10 月 27 日
編集済み: Cedric 2017 年 10 月 27 日
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 件のコメント
O Mueller
O Mueller 2017 年 10 月 27 日
like this it works kind of.
z=[za zb];
M= sum(imag(z(:))~=0);
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 Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by