I am trying to write a script in MATLAB for my class. The scenario is that there are four different types of pens. I only know the total cost of all four pens (total is not actually given to me). I am trying to find the individual cost of each different type of pen. My 3 "friends" also each bought the four pens themselves. That makes for a total of 16 pens among 4 people. Everyone's total cost should be the same. The book suggests creating a matrix for the pens made up of columns for each different type of pen and rows for each of the people (4x4). It also says to have a column vector for the totals each person spent on the pens, which I presume would all be the same. I am stuck and really not sure how to go about solving this since I do not know the cost of even one of the pens. Any help would greatly be appreciated.
@TTT is right, linear algebra solves your task. The great thing about Matlab is, that it can actually calculate linear algebra without the fuzz of building for-loops. Here is a simple example that should suit your case.
Footnote: Note that the matrix inversion with inv()
will be flagged as inefficient by the Matlab-IDE (ie the program) because it is much faster and more accurate to calculate inv(NumPens) * total
jointly (which is expressed as NumPens\\total
) than explicitly calculating the inverse of the matrix first -- but to teach linear algebra, this way is much better!)
total = [17;13;12;27]; % vector 4x1 (number of persons x 1)
NumPens = [1 1 3 1
1 0 1 1
0 1 0 2
3 0 1 1]; % matrix 4x4 (number of persons x number of pen types)
% total = NumPens * x % original system
x = inv(NumPens) * total % how to calculate the number of pens
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.