how get it?

2 次查看(过去 30 天)
Luca Re
Luca Re 2023-10-7
a=magic(5)
a = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
b=[1 3 2 4 5]
b = 1×5
1 3 2 4 5
i want to get:
in 1 column i want to 1 element
in 2 column i want to 3 element
in 3 column i want to 2 element
in 4 column i want to 4 element
in 5 column i want to 5 element
ans:
17 6 7 21 9

采纳的回答

Walter Roberson
Walter Roberson 2023-10-8
a=magic(5)
a = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
b=[1 3 2 4 5]
b = 1×5
1 3 2 4 5
v = a(sub2ind(size(a), b, 1:size(a,2)))
v = 1×5
17 6 7 21 9

更多回答(1 个)

Torsten
Torsten 2023-10-7
编辑:Torsten 2023-10-7
a = magic(5)
a = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
b = [1 3 2 4 5]
b = 1×5
1 3 2 4 5
v = arrayfun(@(i)a(b(i),i),1:size(a,2))
v = 1×5
17 6 7 21 9

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by