list of subjects with at least one in matlab

To list subjects with at least one item in Matlab, you can use logical operators and conditional statements. For example:

main.m
% Define a cell array of subjects and their items
subjects = {'Math' {'Algebra', 'Calculus', 'Geometry'};
            'Science' {'Physics', 'Chemistry', 'Biology'};
            'English' {'Grammar', 'Literature', 'Composition'};
            'History' {'World', 'American', 'European'}};

% Loop through the subjects and check if at least one item exists
for i = 1:size(subjects,1)
    if any(subjects{i,2})
        disp(subjects{i,1})
    end
end
447 chars
13 lines

This code will output:

main.m
Math
Science
English
History
29 chars
5 lines

Because all four subjects have at least one item.

gistlibby LogSnag