Suggestion for modifying code for combining matrices

1 vue (au cours des 30 derniers jours)
Ali
Ali le 14 Juin 2016
Commenté : Ali le 18 Juin 2016
I want to create matrix C, from matrix A and B.
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)
s1, s2, s3 are row’s number in matrix B.
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
this is an example:
A= [4 1 6]
B= [4 1
6 8
1 6
1 4
6 1
6 4
4 6]
C= [3 4 6]
I use the following code, but it gives me an error.
could you please help me with the code.
[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;
error:
Error using bsxfun
Requested 2x80310x80310 (12.0GB) array exceeds maximum array size preference. Creation of
arrays greater than this limit may take a long time and cause MATLAB to become
unresponsive. See array size limit or preference panel for more information.
Error in untitle (line 81)
X = reshape(any([all(bsxfun(@eq,Y1,Bp));all(bsxfun(@eq,Y2,Bp))]),m*n,[])';
  2 commentaires
Stephen23
Stephen23 le 14 Juin 2016
Modifié(e) : Stephen23 le 14 Juin 2016
Ali
Ali le 14 Juin 2016
Okay,
But this is the code that I posted.
It does not work very well and it gives me the error that I mentioned.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 14 Juin 2016
Modifié(e) : Andrei Bobrov le 17 Juin 2016
Hi Ali! Please try it code:
[m,n] = size(A);
out = size([m,n]);
aa = permute(cat(3,A,circshift(A,[0 -1])),[2 3 1]);
for ii = 1:m
for jj = 1:n
a = aa(jj,:,ii);
out(ii,jj) = max([find(ismember(B,a(end:-1:1),'rows'),1,'last'),...
find(ismember(B,a,'rows'),1,'first')])-1;
end
end
  6 commentaires
Andrei Bobrov
Andrei Bobrov le 17 Juin 2016
Modifié(e) : Andrei Bobrov le 17 Juin 2016
Hi Ali, corrected.
Ali
Ali le 18 Juin 2016
Hi Andrei Bobrov,
I really appreciate your help. thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by