How to find all the elements of a row whose one or two column values are entered by a user?

1 回表示 (過去 30 日間)
I have a matrix
A = [
4 5 0.0008 0.0128 0.1342 0 100 345
4 14 0.0008 0.0129 0.1382 0 100 345
5 8 0.0008 0.0112 0.1476 0 100 345
6 5 0.0002 0.0026 0.0434 0 100 345
6 7 0.0006 0.0092 0.113 0 100 345
6 11 0.0007 0.0082 0.1389 0 100 345];
Now, I ask a user to enter any two values between 1 and 15, i.e., I am asking to enter values that belong to the first and second columns only. Suppose one enters the first value as 6 and the second as 5. Now I want that the first value should match a value in the first column and the second value in the second column and I want to know the next two values (i.e. 0.0002, 0.0026) or the next three values (i.e., 0.0002, 0.0026, 0.0434) or all the elements of the row whose first and second column elements are 6 and 5. I got the following answer-
userinput = [6 5]; %obtained however you want
A(all(A(:, [1 2]) == userinput, 2), :) %returns the row(s) for which both columns 1 and 2 match userinput.
But now I want to make it more user friendly. The first value entered by the user might belong to either of the first two columns or neither of them. The same goes with the second value. A user only knows to enter two numbers between 1 and 15. They might belong to either of the first two columns or neither of them or any one value belongs to any one of the two columns. Suppose I have the following cases-
(a) first value - 8, second value - 5
(b) first value - 6, second value - 3
(c) first value - 7, second value - 2
(d) first value - 9, second value - 10In case (a) both the values belong to the matrix though the sequence gets interchanged. But I still want all the elements of the row. Cases (b), (c), (d) do not belong to the matrix. So in such cases I want to display error and might ask the user to enter different values untill it belongs to the matrix.

回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by