Clear Filters
Clear Filters

How can I assign user input values to specific rows and columns in a matrix?

2 views (last 30 days)
A is assigned to the following matrix
2000 1300 900
2001 2100 1800
2002 1450 800
B is assigned to the following matrix
8DB594 1MZ090T
The values in column 1 of A refer to a year.
The values in column 2 of A refer to the production values for device ID "8DB594."
The values in column 3 of A refer to the production values for device ID "1MZ090T."
I am assigned to the task of:
"Prompt the user to select a year and a device ID based on the data provided.
On the command window, output the year, device ID, and production for that year."
If the user enters the year 2001 and device ID 1MZ090T, how do I output the production value for these two values?
In other words, given that the user inputs 2001 and IM1090T and that I have matrix A, how do I assign the entered values to specific rows and columns within matrix A?

Accepted Answer

Stephen23
Stephen23 on 5 Sep 2018
Using a table would be much easier. But given the data in your question, you could do this:
A = [...
2000 1300 900
2001 2100 1800
2002 1450 800];
B = {'8DB594','1MZ090T'};
Ua = 2001; % user input
Ub = '1MZ090T' % user input
Xa = A(:,1)==Ua;
Xb = strcmp(B,Ub);
Z = A(Xa,[false,Xb]) % select the data
  2 Comments
Andrew Padilla
Andrew Padilla on 5 Sep 2018
Stephen,
I appreciate the help. I was able to successfully implement this into my script. If I may ask, how does this work? I have looked up "strcmp" "==" and "[false,someVariable]," but I am still unsure how the three work together to accomplish the desired task.
Thank you again for your time!
Stephen23
Stephen23 on 6 Sep 2018
Both Xa and Xb are logical indices. Logical indices are one of the basic ways of accessing data in an array:
I used strcmp to generate a logical index of where the user input Ub matches the char vectors in B.
I used == to generate a logical index of wherethe user input Ua equals the values of the first column of A.
These two logical indices are them used to obtain the value from A. Xb padded with false because columns two and three contain the data corresponding to the first and second char vectors in B, so we need to offset the indexing by one (for which I used false).

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!