Replacing values in array with values from another array

조회 수: 4 (최근 30일)
L'O.G.
L'O.G. 2022년 7월 17일
댓글: Star Strider 2022년 7월 17일
The values in each row of an array correspond to the values i, j of an upper triangular array. How do I return a vector with elements that are the corresponding i, j value in the array? For example:
test_array = [1 2; 2 3; 1 3];
test_upper_triangular_array = [0.5 0.25 0.7; 0 0.8 0.6; 0 0 1.1];
desired_vector = [0.25 0.6 0.7]

채택된 답변

Star Strider
Star Strider 2022년 7월 17일
편집: Star Strider 2022년 7월 17일
Try this —
test_array = [1 2; 2 3; 1 3];
test_upper_triangular_array = [0.5 0.25 0.7; 0 0.8 0.6; 0 0 1.1]
test_upper_triangular_array = 3×3
0.5000 0.2500 0.7000 0 0.8000 0.6000 0 0 1.1000
lidx = sub2ind(size(test_upper_triangular_array), test_array(:,1), test_array(:,2)) % Convert Subscripts To Linear Indices
lidx = 3×1
4 8 7
result = test_upper_triangular_array(lidx)
result = 3×1
0.2500 0.6000 0.7000
% desired_vector = [0.25 0.6 0.7]
EDIT —
Transpose to get row vector:
result_row = result.'
result_row = 1×3
0.2500 0.6000 0.7000
.
  댓글 수: 3
Star Strider
Star Strider 2022년 7월 17일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by