How to modify this code for combining matrices

1 次查看(过去 30 天)
I want to use matrix A and B to create matrix C.
I would like to modify my code to work well for this problem.
Any help and suggestion is appreciated.
A = (r1 r2 r3)
B=
[r1 r2
r3 r4
r2 r1
r5 r6
r2 r3
r3 r2
r3 r1
r1 r3]
C=(s1 s2 s3)
Matrix A and matrix C have the same size.
s1, s2, s3 are row’s number in matrix B. The row’s number in matrix B start from 0.
s1= bigger row’s number between r1 r2 and r2 r1
s2= bigger row’s number between r2 r3 and r3 r2
s3= bigger row’s number between r3 r1 and r1 r3

采纳的回答

Andrei Bobrov
Andrei Bobrov 2016-6-2
[m,n] = size(A);
Y1 = reshape(permute(cat(3,A,circshift(A,[0 -1])),[3,2,1]),2,[]); %
Y2 = flip(Y1,1);
Bp = permute(B,[2,3,1]);
X = reshape(any([all(bsxfun(@eq,Y1,Bp));all(bsxfun(@eq,Y2,Bp))]),m*n,[])';
C = reshape(max(bsxfun(@times,X,(1:size(B,1))')),[],m)'-1;
  2 个评论
Ali
Ali 2016-6-2
Dear Andrei, You saved me, it was a vital stage in my academical life. many thanks, wish the best for your life Best Regard

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by