필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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?

채택된 답변

Stephen23
Stephen23 2018년 9월 5일
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
Andrew Padilla
Andrew Padilla 2018년 9월 5일
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 2018년 9월 6일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by