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 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