searching for vector values in a matrix

4 ビュー (過去 30 日間)
Ahmed Alnahdi
Ahmed Alnahdi 2015 年 4 月 28 日
コメント済み: Ahmed Alnahdi 2015 年 4 月 28 日
Hi, I have a vector with 3 specific values and I want to find their indices in a matrix.
  1 件のコメント
James Tursa
James Tursa 2015 年 4 月 28 日
What are the dimensions of the matrix? Do the values have to be next to each other in the same row or column?

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

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 4 月 28 日
編集済み: Mohammad Abouali 2015 年 4 月 28 日
% Creating a sample matrix
yourMatrix=reshape(1:15,5,3)
yourMatrix =
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
% Creating a sample vector with three values.
yourVector=[1,7,14];
% Now getting the row and cols of those values.
[rows, cols]=ind2sub( size(yourMatrix), ...
arrayfun(@(v) find(yourMatrix==v), ...
yourVector(:)) )
rows =
1
2
4
cols =
1
2
3
The rows and cols of 1,7,14 (the three specific values) are provided. If you are looking only for linear index number then just run:
linIndex=arrayfun(@(v) find(yourMatrix==v), yourVector(:))
  1 件のコメント
Ahmed Alnahdi
Ahmed Alnahdi 2015 年 4 月 28 日
ماتقصر :)

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 4 月 28 日
If yourVector is an integer valued vector or a floating point vector with values that are multiples or divisors of 2, then try
firstIndex = find(yourVector == firstSpecificValue);
secondIndex = find(yourVector == secondSpecificValue);
thirdIndex = find(yourVector == thirdSpecificValue);

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by