Comparing arrays using loop?

2 views (last 30 days)
Anonymous Matrix
Anonymous Matrix on 23 Feb 2017
Commented: Anonymous Matrix on 23 Feb 2017
I need some help on assigning some codes. Say I have three matrices.
Matrix1= [cat;dog;hamster]
Matrix2=[7,6,1,5,8,1,9;4,6,10,15,8,1,9;3,6,25,5,8,4,10] %(this matrix represents how long a person watched them in their container).
Matrix3= [4,6,1,1,3,1,6;4,2,3,1,4,1,6;3,6,10,5,4,4,10] %(this matrix reprsents how many pets sold).
And each row in Matrix2 and Matrix 3 corresponds to the animal in that row in Matrix1 (ie. row 3 in Matrix2 and Matrix3 is for the hamster). How do i write a loop that says that if the total time watched is over 50 and amount sold is over 30 means that that pet is the most popular? I'm mainly stuck on where to put the FOR loop index (ie. for x=1:3)

Accepted Answer

Peter O
Peter O on 23 Feb 2017
Edited: Peter O on 23 Feb 2017
You can actually do this in Matlab without needing a for loop. If I'm interpreting your question correctly, you're asking to find which row in a matrix contains the maximum sum. To achieve this, use the max and sum commands.
totWatch = sum(Matrix2,2); %Sum along dimension #2 (columns)
totBought = sum(Matrix3,2);
This gives us two vectors of 3x1 (cat, dog, hamster). Let's find which have a watch count above 50 AND a sell count above 30:
isPopular = (totWatch > 50) & (totBought > 30)
isPopular =
0
0
1
So strangely, hamsters are more popular than dogs. To find the most popular of each in an automated fashion you can use the max command, which can give you both the max value and the index of that value. For the most bought, for instance:
[maxB, ixB] = max(totBought)
maxB =
42
ixB =
3
Tells us that 42 pets of one type were sold, and this occurred in Row 3, the hamsters.
(edit: spelling)
  3 Comments
Peter O
Peter O on 23 Feb 2017
Then I suspect you have a coding homework assignment ;-)
I won't give you a direct solution to your assignment, but let's deconstruct how MATLAB's indexing works and how we could apply it to the problem.
So what are we looking to do? We'll take the first part of the above solution. We have to look at each row, and then within each row, add up the columns. The indexing in Matlab to access a particular element within a matrix "A" looks like:
A(row, col)
Using for loops, we can iterate over the rows:
for ix=1:3
the_row = A(ix,:); % The colon ':' is ML shorthand for "get all the entries"
disp(the_row);
end
We can also iterate over columns:
for ix=1:6
the_col = A(:,ix); % "All the entries" now means all rows
disp(the_col);
end
We can iterate over all the rows, and inside that, iterate over the columns to get single entries. When we place one loop inside of another, it's called a nested loop. We'll also need a second iterator variable.
for ix=1:3
% Do something each time you get a new row
for jx=1:6
the_element = A(ix,jx);
disp(the_element);
% do other things with element
end
%do some stuff once we've accessed all elements in this row
end
Hopefully that helps you with your assignment!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!