Question about logical indexing

4 views (last 30 days)
Angelo Spadaro
Angelo Spadaro on 4 Mar 2019
Answered: Aditya on 20 Jan 2025
I am relatively new to MatLab and am having a tough time with organizing a set of data for work. I have the data in two separate tables. The first table is a master list of traps and cumulative catch per trap with variable names: Year (using integers in place of actual years), Site (again, integers instead of site names), Week (integers again), Trap (integer - unique Tag ID), Catch (response measured - count data). Example below:
Year Site Week Trap Catch
____ ____ ____ ____ _____
1 1 1 29 9
1 1 1 28 0
1 1 1 18 4
1 1 1 27 0
1 1 1 622 19
1 1 1 10 0
1 1 1 17 10
The second table is the raw data describing each individual animal collected in each trap. Example with variable names below:
Year Week Site Trap Sex CL Legal Greater60
____ ____ ____ ____ ___ ____ _____ _________
1 1 2 41 2 71.1 0 1
1 1 2 41 2 50 0 0
1 1 2 42 2 73.9 0 1
1 1 2 42 2 57.3 0 0
1 1 2 42 2 61.5 0 1
Year, Week, Site, and Trap all appear in both tables. In table two. Catch, in table 1 is the total number of animals caught by each unique trap in a year, week, site combination. Legal and Greater60 in table two are binary responses based on the size of the animal (1 = true, 0 = false). All traps in Table two appear in table one, but if a trap did not catch anything, it does not appear in table two.
My goal is to use logical indexing to add two columns (Legal, Greater60) to Table 1 after Catch wherein I have a sum of the Legal and Greater60 values in Table two for each trap in each year, week, site combination.
I hope that makes sense. Any help is much appreciated.

Answers (1)

Aditya
Aditya on 20 Jan 2025
Hi Angelo,
To achieve your goal of adding "Legal" and "Greater60" columns to Table 1, you can use the "groupsummary" and "outerjoin" functions in MATLAB. Here's a concise solution:
% Summarize Legal and Greater60 for each unique trap in Table 2
summaryTable = groupsummary(table2, {'Year', 'Site', 'Week', 'Trap'}, 'sum', {'Legal', 'Greater60'});
The "groupsummary" function is used to efficiently calculate the sum of Legal and Greater60 values for each unique combination of "Year", "Site", "Week", and "Trap" in Table 2. This function helps in aggregating data based on specified groups.
% Merge the summarized data with Table 1
resultTable = outerjoin(table1, summaryTable, 'Keys', {'Year', 'Site', 'Week', 'Trap'}, ...
'MergeKeys', true, 'Type', 'left');
The "outerjoin" function is then used to merge this summarized data with Table 1. It ensures that all rows from Table 1 are retained, even if there are no corresponding entries in Table 2, by performing a left join. This is important to maintain the structure of Table 1 while adding the new columns.
To read more about this functions, refer to the below MATLAB documentation:
I hope this helps!

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!