Restricting a set of data by a spherical radius

1 回表示 (過去 30 日間)
jgillis16
jgillis16 2015 年 8 月 3 日
編集済み: jgillis16 2015 年 8 月 10 日
I need to restrict column 3 of the attached text file by a spherical radius with a central number of 12.51 that has a radius that extends 0.7333 in every direction.
  6 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 3 日
What do you want to happen for cases that do not match that?
Is "dec" intended to convey "declination", and is "ra" intended to convey "right ascension" (instead of "radius" for example) ?
What formula should be used to test to see if a value is in range?
jgillis16
jgillis16 2015 年 8 月 4 日
I have some octave code as a reference... but I don't know how to implement it into matlab (but this code should take care of this problem)
function d=angulardist(ra1,dec1,ra2,dec2)
error(mynargchk(4,4,nargin,"struct"));
if common_size(ra1,dec1) && common_size(ra2,dec2)
error("angulardist: input vector dimensions don't match");
endif
if !common_size(ra1,ra2)
d=sind(dec1).*sind(dec2)+cosd(dec1).*cosd(dec2).*cosd(ra1-ra2);
elseif columns(ra1)==1 && rows(ra2)==1
d=sind(dec1)*sind(dec2)+(cosd(dec1)*cosd(dec2)).*cosd(repmat(ra1,1,length(ra2))-repmat(ra2,length(ra1),1));
else
error("angulardist: input vector dimensions don't match");
endif
idx=find(d>1+eps);
d=real(acosd(d));
if ~isempty(idx)
warning("angulardist: irregular angular coordinates");
d(idx)=nan;
endif
endfunction
function out=selectobjectsinfield(in,RA,DEC,fieldDesc,rejectflag)
error(mynargchk(4,5,nargin,"struct"));
if nargin<5
rejectflag=false;
endif
if isscalar(fieldDesc)
circle=true;
radius=fieldDesc;
else
circle=false; # rectangle
RAwidth=fieldDesc(1);
DECwidth=fieldDesc(2);
endif
ra=[in(:).RA];
dec=[in(:).dec];
if circle==true
angularDist=angulardist(ra,dec,RA,DEC);
if rejectflag
idx=find((abs(angularDist)>=radius));
else
idx=find((abs(angularDist)<=radius));
endif
else
RADist=angulardist(ra,dec,RA,dec);
decDist=angulardist(ra,dec,ra,DEC);
if rejectflag
idx=find((abs(RADist)>=RAwidth/2)&(abs(decDist)>=DECwidth/2));
else
idx=find((abs(RADist)<=RAwidth/2)&(abs(decDist)<=DECwidth/2));
endif
endif
out=in(idx);
endfunction

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 4 日
function d=angulardist(ra1,dec1,ra2,dec2)
if ~(isscalar(ra1) || isscalar(dec1) || isequal(size(ra1),size(dec1)) &&
~(isscalar(ra2) || isscalar(dec2) || isequal(size(ra2),size(dec2))
error("angulardist: input vector dimensions don't match");
end
if ~(isscalar(ra1) || isscalar(ra2) || isequal(size(ra1),size(ra2))
d = sind(dec1).*sind(dec2)+cosd(dec1).*cosd(dec2).*cosd(ra1-ra2);
elseif size(ra1,2)==1 && size(ra2,1)==1
d = sind(dec1)*sind(dec2)+(cosd(dec1)*cosd(dec2)).*cosd(repmat(ra1,1,length(ra2))-repmat(ra2,length(ra1),1));
else
error("angulardist: input vector dimensions don't match");
end
idx = find(d>1+eps);
d = real(acosd(d));
if ~isempty(idx)
warning("angulardist: irregular angular coordinates");
d(idx)=nan;
end
end
function out = selectobjectsinfield(in,RA,DEC,fieldDesc,rejectflag)
if nargin<5
rejectflag=false;
end
if isscalar(fieldDesc)
circle = true;
radius = fieldDesc;
else
circle = false; # rectangle
RAwidth = fieldDesc(1);
DECwidth = fieldDesc(2);
end
ra = [in(:).RA];
dec = [in(:).dec];
if circle == true
angularDist = angulardist(ra,dec,RA,DEC);
if rejectflag
idx = find((abs(angularDist)>=radius));
else
idx = find((abs(angularDist)<=radius));
end
else
RADist = angulardist(ra,dec,RA,dec);
decDist = angulardist(ra,dec,ra,DEC);
if rejectflag
idx = find((abs(RADist)>=RAwidth/2)&(abs(decDist)>=DECwidth/2));
else
idx = find((abs(RADist)<=RAwidth/2)&(abs(decDist)<=DECwidth/2));
end
end
out = in(idx);
end
  2 件のコメント
jgillis16
jgillis16 2015 年 8 月 4 日
Thanks! But, I come across an error
"Error: File: angular.m Line: 7 Column: 79
Expression or statement is incorrect--possibly unbalanced (, {, or [."
It's the first set of '&&' that is causing the issue.
Walter Roberson
Walter Roberson 2015 年 8 月 4 日
Change the first part to
if ~(isscalar(ra1) || isscalar(dec1) || isequal(size(ra1),size(dec1))) &&
~(isscalar(ra2) || isscalar(dec2) || isequal(size(ra2),size(dec2)))
error("angulardist: input vector dimensions don't match");
end
if ~(isscalar(ra1) || isscalar(ra2) || isequal(size(ra1),size(ra2)))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by