Search indecies in matrix

1 回表示 (過去 30 日間)
bob k
bob k 2015 年 10 月 16 日
編集済み: bob k 2015 年 10 月 20 日
thank you very much

採用された回答

the cyclist
the cyclist 2015 年 10 月 16 日
l1 = {'A' 'B';
'B' 'A';
'B' 'C';
'C' 'D';
'D' 'E';
'E' 'F';
'F' 'E';
'F' 'C';
'C' 'F';
'F' 'B';
'B' 'F';
'F' 'G';
'G' 'F';
'G' 'A';
'G' 'E';
'E' 'G'};
x = {'B' 'C' 'D' 'E' 'F' 'G'};
numberRows = size(l1,1);
numberCols = size(x,2) + 1; % Offsetting by one, to compensate for the 0 values in loc. Will chop that off later.
[~,loc] = ismember(l1,x);
loc1 = loc(:,1)+1;
loc2 = loc(:,2)+1;
A = zeros(numberRows,numberCols);
linearIndex1 = sub2ind(size(A),1:numberRows,loc1');
linearIndex2 = sub2ind(size(A),1:numberRows,loc2');
A(linearIndex1) = -1;
A(linearIndex2) = 1;
% Chop off spurious first column
A(:,1) = [];

その他の回答 (1 件)

bob k
bob k 2015 年 10 月 16 日
Hi 'the cyclist',
Thank you very much, I could not figure that out for the life of me. I really appreciate it.
  1 件のコメント
the cyclist
the cyclist 2015 年 10 月 17 日
編集済み: the cyclist 2015 年 10 月 17 日
I'm happy to have helped!
Two quick comments:
  1. The best form of thanks is upvoting and/or accepting the solution, which rewards the person who answered, and also may help guide folks who may have similar questions.
  2. You added this remark as a new "answer", when it would have been better placed as a comment on my answer.

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

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by