How to do a lookup for the best number that suits a condition between two arrays?

조회 수: 1 (최근 30일)
I have two vector arrays "Gap" and "Shim" respectively. What I expect from Matlab is that
  1. it first takes the first value from Gap
  2. Perform an if check "if 0<[Gap - Shim]<0.05"
  3. If yes, "Shim" is the answer I am looking for Gap 1
  4. If not, keep jumping to next shim(s) until the condition 0<[Gap-Shim]<0.05 is met
  5. This "Shim x" is the answer I am looking for Gap 1
  6. Repeat this for all values of Gap
  7. Give a table of respective Shims for all the Gap values
These are my inputs:
Gap_range=0.28:0.01:0.5
Shim_range = 0.25:0.05:0.5
I don't how to proceed anything further owing to my lack of experience with Matlab. Pl. help.

답변 (1개)

Peter Perkins
Peter Perkins 2022년 3월 24일
If you create gap as a row, and shim as a column, this is only a couple of lines. < will automatically expand the two vectors to match each others' sizes. It's known as "implicit expansion". So use that to make an MxM logical matrix, and find the one true in each column. The row numbers of those trues are your shims.
x = 1:3;
y = (1:3)';
x - y
ans = 3×3
0 1 2 -1 0 1 -2 -1 0
0 <= (x-y) & (x-y) <= .5
ans = 3×3 logical array
1 0 0 0 1 0 0 0 1

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by