How to find particular elements of matrix?

1 回表示 (過去 30 日間)
Ammy
Ammy 2021 年 8 月 22 日
コメント済み: Walter Roberson 2021 年 8 月 22 日
A=reshape(1:36,6,6);
How to select the following elements of matrix
7,25,
2,14,20,32
9,27
10,28
5,17,23,35
12,30
these can be find like this
A(1,2)=7, A(1,4)=25....
But is there any generalized way which help even for large matrices.

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 22 日
A=reshape(1:36,6,6)
A = 6×6
1 7 13 19 25 31 2 8 14 20 26 32 3 9 15 21 27 33 4 10 16 22 28 34 5 11 17 23 29 35 6 12 18 24 30 36
targets = {
[7,25]
[2,14,20,32]
[9,27]
[10,28]
[5,17,23,35]
[12,30]
}
targets = 6×1 cell array
{[ 7 25]} {[2 14 20 32]} {[ 9 27]} {[ 10 28]} {[5 17 23 35]} {[ 12 30]}
for K = 1 : length(targets)
[~, locations{K}] = ismember(targets{K}, A(K,:));
end
celldisp(locations)
locations{1} = 2 5 locations{2} = 1 3 4 6 locations{3} = 2 5 locations{4} = 2 5 locations{5} = 1 3 4 6 locations{6} = 2 5
  3 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 22 日
Could be but you don't seem to have any regular recipe like that. The indexes you gave seem to be fairly random with no obvious formula to get them. However you gave only two locations (1,4) and (1,4) so that's not enough to detect any pattern.
If you need anymore help, upload your values in a matrix, like
linearIndexes = [7,25,...
2,14,20,32,...
9,27,...
10,28,...
5,17,23,35,...
12,30]
all in a row vector (did I do it right?), and give more than 2 location for where to assign those values.
Walter Roberson
Walter Roberson 2021 年 8 月 22 日
You have two index patterns:
  1. columns 2 and 5
  2. columns 1, 3, 4, 6
However, it is not obvious which pattern is to be applied to which row. The longer extracts are rows 2 and 5, which happens to match the column numbers for the shorter rows, but will that always be the case?
You talk about larger matrices, but do not give us any information about how the accesses will generalize. For example are we to deduce that 2, 5 is "second and second-last" ? Are we to deduce that 1, 3, 4, 6 is "first, two middle, last", or are we to deduce that it "everything not selected by the previous pattern" ?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by