Find common non-zero indexes

2 ビュー (過去 30 日間)
Tha saliem
Tha saliem 2017 年 6 月 10 日
コメント済み: Andrei Bobrov 2017 年 6 月 12 日
i have a matrix like this:
A=[4,0,5,5;0,2,4,0;3,0,0,0;0,1,0,3;3,0,1,0;2,0,0,2;0,0,3,0;2,3,4,0]
And matrix B and C are row numbers derived from matrix A based on some condition.
B=[7;8]
C=[1;2;3;4;5;6]
I want to compare values of matrix B with each value of matrix C and find what nonzero indexes are common between both in matrix A.
for example: first value of B is 7 and it will be compared with all values of C. In first iteration, comparison will be 7 and 1 and result will be 3 because 3rd value is common nonzero value between both. Similarly:
result(7,2)=3
result(7,3)=0 (No nonzero index is common)
and similarly after 7, 8 will be compared with all values of C.
Please help on this.
  1 件のコメント
Tha saliem
Tha saliem 2017 年 6 月 10 日
Also it is not necessary that values in B and C are sorted. It can be in any order

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 10 日
編集済み: Andrei Bobrov 2017 年 6 月 12 日
[EDIT 2]
A=[4,0,5,5;0,2,4,0;3,0,0,0;0,1,0,3;3,0,1,0;2,0,0,2;0,0,3,0;2,3,4,0];
B=[7;8];
[C,~] = find(bsxfun(@ne,(1:size(A,1))',B(:)'));
n = numel(B);
B1 = repelem(B,numel(C)/n);
k = squeeze(num2cell( bsxfun(@times,bsxfun(@and,A(C,:), A(B1,:)),1:size(A,2)),2 ));
result = cellfun(@nonzeros,k,'un',0);
result(cellfun(@isempty,result)) = {0}
result = reshape(result,[],n);
  6 件のコメント
Tha saliem
Tha saliem 2017 年 6 月 10 日
編集済み: Tha saliem 2017 年 6 月 11 日
Thanks alot for helping. it gives following error:
Error using ~= Matrix dimensions must agree.
Error in test10 (line 31) [C,~] = find((1:size(A,1))' ~= B(:)');
Andrei Bobrov
Andrei Bobrov 2017 年 6 月 12 日
Second correction

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

その他の回答 (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