Data sorting using logic

6 views (last 30 days)
Yang Hu
Yang Hu on 10 Oct 2023
Commented: Yang Hu on 10 Oct 2023
Hello everyone, I am using R2023a to do data sorting using logic to remove some data, save and output the other part of the data. The original data is attached, called 'IndiMSD', with a size of 97 by 107. I want to first examine the last element of each column to see if they are below 5000 or exceed 60000, it they are, they will be excluded; it not, they will be included to be saved and used for furthur use.
Here are my codes for now,
I think I can examine the last element of each column to see if they are within the range, and do a logic calculation. For this data set, my code can cut down from 107 to 95 after the doing the logic. But when I try to sort out the data for the columns with a logical value 1, I am having some trouble. The sorted out data gives a 95 by 97 matrix but starting from the first column without identifying 'logic = 0' columns. Can someone help me on this to let it only output the columns that are logic 1, not from the beginning?
Thank you.
goodRows1 = logical(iswithin(IndiMSD(97,:),5000,60000));% examine last element of each column
logic_MSDwithin = goodRows1(:,goodRows1); % compare to the range
IndiMSD_within4 = IndiMSD(:, logic_MSDwithin);% output data that are logical value is 1

Accepted Answer

dpb
dpb on 10 Oct 2023
Edited: dpb on 10 Oct 2023
goodRows1 = logical(iswithin(IndiMSD(97,:),5000,60000));% examine last element of each column
logic_MSDwithin = goodRows1(:,goodRows1); % compare to the range
IndiMSD_within4 = IndiMSD(:, logic_MSDwithin);% output data that are logical value is 1
This looks suspiciously like you've picked up from an Answer I gave some time earlier-- iswithin() is a utility function I've posted many times before as being useful "syntactical sugar" to move the more complex comparison code to a lower level resulting in less clutter at the top level. It is given as
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
You will observe from the help that it "returns T for values within range..."; "T" is shorthand for "True" and is already a logical value. Hence, the logical() cast operation in the first line of code is totally redundant; it can't get in more logical than logical. :)
goodRows1=iswithin(IndiMSD(end,:),5000,60000); % examine last element of each column
The above replaces the hardcoded "97" with the internal "end" function that returns the last index for the array when used as above within an indexing expression. That makes the code much more useful in that it doesn't rely on there being exactly that many records and you can use it for many files without changing the code itself.
I'm not clear on just what is your end intent; if the intent is then to retain the columns which are within those ranges for the last elemtn in the columns, then you're almost done --
IndiMSD_within4 = IndiMSD(:, goodRows1); % retain only those identified columns
As a matter of style although it makes absolutely no difference in the functionality, the variable naming isn't very good above, however, it it is indeed the columns you're trying to keep; those are "goodColumns", not "goodRows" even though you made the decision using just one row.
I'm one who also uses mostly very short variable names, and partictularly for temporary variables like the above addressing logical array; I would have almost certainly written something more like
ix=iswithin(IndiMSD(end,:),5000,60000); % index to columns within range based on last row values
IndiMSD_within4 = IndiMSD(:,ix); % retain only those identified columns
or, as often there may not be any further need for the logical addressing vector, I may dispense with it entirely...
% select columns within range based on last row values
IndiMSD_within4 = IndiMSD(:,iswithin(IndiMSD(end,:),5000,60000));
If I missed the intent of the question, post back and let know where went wrong...
  1 Comment
Yang Hu
Yang Hu on 10 Oct 2023
Yes! I came across your iswithin functino and tried to adapt my data from it! You are the man, the code you provided works perfectly and runs smoothly. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!