How to set an upper and lower limit on a data set from a matrix

조회 수: 9 (최근 30일)
jgillis16
jgillis16 2015년 8월 3일
댓글: jgillis16 2015년 8월 4일
I need to set an upper and lower limit on the numbers in a data set that belongs to a matrix.
How would I go about doing this?
  댓글 수: 2
James Tursa
James Tursa 2015년 8월 3일
Please provide a short example showing inputs and desired output.
jgillis16
jgillis16 2015년 8월 3일
I have a data set that goes from 0 to 360 that is apart of a matrix called GalList >>> GalList(:,3). I need to extract values ranging only from 176.71 to 198.71.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 8월 3일
at_least = -5.1234; %for example
at_most = 308.42; %for example
new_matrix = min(max(YourMatrix, at_least), at_most);
Now new_matrix has had every value less than at_least replaced with at_least, and every value greater than at_most replaced with at_most.

추가 답변 (1개)

James Tursa
James Tursa 2015년 8월 4일
Assuming you want to extract all the rows where the 3rd column meets your conditions:
GalList = your matrix
lower_limit = 176.71;
upper_limit = 198.71;
x = (GalList(:,3) >= lower_limit) & (GalList(:,3) <= upper_limit);
extracted_rows = GalList(x,:);
  댓글 수: 3
James Tursa
James Tursa 2015년 8월 4일
Do you mean read in only those lines, or create a new file with only those lines, or what? How are you currently reading this file?
jgillis16
jgillis16 2015년 8월 4일
Currently, my code looks like the following:
load virgoclusterrm.txt %loads text into workspace
readCatalog( virgoclusterrm )
fid = fopen( 'virgoclusterrm.txt');
trashLine = fgets(fid); %Skips the first line
data = textscan(fid, '%f%s%f%f%s%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Delimiter', '|', 'TreatAsEmpty','~');
fclose(fid);
GalList.pgc = data{1};
GalList.name = data{2};
GalList.ra = data{3};
RA = GalList.ra.*15;
GalList.dec = data{4};
GalList.major = data{7};
GalList.abs_mag = data{14};
GalList.dist = data{15};
lower_limit = 176.71;
upper_limit = 198.71;
x = (RA >= lower_limit) & (RA <= upper_limit);
extracted_rows = RA(x,:);
RAo = extracted_rows./15;
RAo contains the values I restricted column 3 to. But, column 3 was originally derived from the text file, virgoclusterrm.txt. Now, I have these 877 values but I want to correlate them back to their respective lines of text.
Would there be any way to scan the original text file for the values in RAo in column three, and extract any matches into a new text file?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by