Matlab corresponding values between matrices?

조회 수: 1 (최근 30일)
Ilias Thanos
Ilias Thanos 2020년 1월 26일
댓글: Turlough Hughes 2020년 1월 27일
So i have these two matrices:
A =
745 780 800 860 810
780 780 760 695 815
744 825 720 690 750
715 860 700 670 620
B =
30 18 40 58 26
70 19 72 29 47
14 48 40 14 48
13 72 59 66 72
I want the user to enter a number N so the program will show how many of A's values are lower than the number the user typed. I did this with this command:
N=input('Type a number :')
C= nnz(A<N)
But now i want the program to show me the values of B that correspond to the A's values that are lower than the N number
(Sorry about my bad english, any ideas?)

채택된 답변

Image Analyst
Image Analyst 2020년 1월 27일
Try this:
A = [...
745 780 800 860 810
780 780 760 695 815
744 825 720 690 750
715 860 700 670 620]
B = [...
30 18 40 58 26
70 19 72 29 47
14 48 40 14 48
13 72 59 66 72]
% N = input('Type a number : ')
N = 745;
% Count how many are below the threshold.
mask = A < N
sumBelowThreshold = nnz(mask)
% Get the values at the same locations in B.
% First as a list:
valuesBelowThreshold = B(mask)
% Then as a masked array
Bmasked = B .* mask
You'll see in the command window:
sumBelowThreshold =
8
valuesBelowThreshold =
14
13
40
59
29
14
66
72
Bmasked =
0 0 0 0 0
0 0 0 29 0
14 0 40 14 0
13 0 59 66 72

추가 답변 (1개)

Turlough Hughes
Turlough Hughes 2020년 1월 26일
편집: Turlough Hughes 2020년 1월 27일
This will show values of B that exist in A and which are also less than N.
result = B(ismember(B,A) & B<N)
If you need find the elements of B that exist in A within a certain tolerance there is also a function called ismembertol which will be appropriate in that case.
  댓글 수: 4
Ilias Thanos
Ilias Thanos 2020년 1월 27일
Oh that's probably my bad cuz I wasnt clear enough. I'll edit the question. I want the program to show the B values that correspond to the A values less than N but more specific that correspond to their positions.
For example if the answer is 745 i want the program to show me 30 (see the matices above)
Turlough Hughes
Turlough Hughes 2020년 1월 27일
Ah ok, no worries. I see what you mean now.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by