kfupm
kfupm

Reputation: 3

some matrix operations and extracting data

I want to ask a question in some matrix operations in MATLAB.

Assume we have this matrix:

A = [1     1    17
     1     1    14
     1     2    10
     1     2    11
     2     1     9
     2     1     9
     2     2    13
     2     2    12
     3     1    18
     3     1    15]

I want the first column, say M and the second column, say D to control the entire matrix to result to one row matrix depending on the following condition:

the program will ask the user to enter the values of M then D as follows:

M = input(' ENTER M VALUE =  ') ;

D = input(' ENTER D VALUE =  ') ;

Now, the output will be the corresponding 2 values to M and D, and these two values will be taken from the third column,

for example:

if M = 1  and  D = 2 , the output is B = 10 ; 11

another example:

if M = 3  and  D = 1 , the output is B = 18 ; 15

and so on.

Actually, I know how to solve this using if statement but I have large data and this will take very long time. I am sure that there is a short way to do that.

Thanks.

Upvotes: 0

Views: 660

Answers (1)

Jonas
Jonas

Reputation: 74940

The short way to do it is

B = A(A(:,1)==M & A(:,2)==D, 3);

Upvotes: 2

Related Questions