how to get the row and column number of a specific value in an array

조회 수: 14 (최근 30일)
D:
42 24 20 17 20 24 19 18 18 15
39 21 19 17 20 18 18 19 15 18
32 20 14 13 17 16 15 15 14 16
27 19 11 12 12 20 14 13 17 18
26 14 10 11 13 16 11 12 16 17
24 10 14 16 19 9 12 17 18 21
e=[];
e(1)=min([D(1,end-1),D(2,end-1),D(2,end)]);
The end operator must be used within an array index expression.
how do i get the row and column number of e(1) in D

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 8월 25일
hello
my suggestion :
it gives row = 2 and col = 9 (e = 15)
D = [42 24 20 17 20 24 19 18 18 15;
39 21 19 17 20 18 18 19 15 18;
32 20 14 13 17 16 15 15 14 16;
27 19 11 12 12 20 14 13 17 18;
26 14 10 11 13 16 11 12 16 17;
24 10 14 16 19 9 12 17 18 21;];
[m,n] = size(D);
ind_x = [1 2 2];
ind_y = [n-1 n-1 n];
e=[];
[e(1),ind] = min([D(ind_x(1),ind_y(1)),D(ind_x(2),ind_y(2)),D(ind_x(3),ind_y(3))]); % [M,I] = min(X,[],...,'linear') returns the linear indices of the minimum values in vector I.
% results :
row = ind_x(ind);
col = ind_y(ind);

추가 답변 (1개)

Kevin Holly
Kevin Holly 2021년 8월 25일
편집: Kevin Holly 2021년 8월 25일
D=[
42 24 20 17 20 24 19 18 18 15
39 21 19 17 20 18 18 19 15 18
32 20 14 13 17 16 15 15 14 16
27 19 11 12 12 20 14 13 17 18
26 14 10 11 13 16 11 12 16 17
24 10 14 16 19 9 12 17 18 21];
e=[];
[e(1) index]=min([D(1,end-1),D(2,end-1),D(2,end)]);
if index == 1
row = 1;
column = length(D(1,:))-1;
end
if index == 2
row = 2;
column = length(D(2,:))-1;
end
if index == 3
row = 2;
column = length(D(2,:));
end
row
row = 2
column
column = 9

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by