How to retrieve location values in a matrix

5 ビュー (過去 30 日間)
Danish Nasir
Danish Nasir 2021 年 7 月 24 日
コメント済み: Danish Nasir 2021 年 7 月 24 日
Suppose i have a matrix 4x6 i.e. 6 columns and 4 rows. I want to extract different location values from the matrix i.e. 3rd location from column 1, 4th location value from column 2, 1st location value from column 3 etc. ([ 3 4 1 5 2 2]). Then i have to add these column location values. Pls suggest the code.

採用された回答

Chunru
Chunru 2021 年 7 月 24 日
% Generate a 4X6 matrix
a = randi(10, 4, 6)
a = 4×6
7 6 6 8 8 6 5 6 4 7 5 8 2 8 6 7 1 1 2 6 9 3 6 6
% The index to each column
%idx = [ 3 4 1 5 2 2] % 5 is not correct
idx = [ 3 4 1 4 2 2];
% Fund the corresponding linear index
ii = sub2ind([4, 6], idx, 1:6);
% Show the extracted data
a(ii)
ans = 1×6
2 6 6 3 5 8
sum(a(ii))
ans = 30
  1 件のコメント
Danish Nasir
Danish Nasir 2021 年 7 月 24 日
Actually i want to take indices of matrix a (4x6), and want to extract the values of some other matrix b (4x6) of same size with these indices.

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 24 日
% e.g.:
A = randi([0, 13], 4,6)
A = 4×6
8 12 8 6 10 4 9 2 1 3 7 1 2 6 5 5 0 1 3 3 10 10 9 1
A(3,1), A(4, 2), A(1,3)
ans = 2
ans = 3
ans = 8

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by