Find the index of a value closest to a constant value in 3D array when the index of row and column are given

조회 수: 9 (최근 30일)
I have a 3D array B(i,j,k). and I want to locate the index (k) in the array closest to the a number Val = 6.
i = 1, j = 4. and the matrix is given as B(1,4,:)
The array is given by:
B(:,:,1) =
1 2 4 2
7 8 9 0
B(:,:,2) =
9 2 4 7
0 1 2 3
B(:,:,3) =
7 8 9 0
1 7 6 8
The value closest to 6 is 7 and the index is 2.
Please, how can I write the code to compute this task.

채택된 답변

Chunru
Chunru 2021년 10월 9일
B(:,:,1) =[ 1 2 4 2
7 8 9 0];
B(:,:,2) =[ 9 2 4 7
0 1 2 3];
B(:,:,3) =[ 7 8 9 0
1 7 6 8];
i = 1; j = 4;
[~, k] = min(abs(B(i,j,:)-6))
k = 2

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by