Effacer les filtres
Effacer les filtres

Do a cross-set operation on a matrix

1 vue (au cours des 30 derniers jours)
Vinay Pandey
Vinay Pandey le 10 Avr 2012
I have a matrix which looks as follows:
[1 2 3;
4 5 6]
I want to do a cross-set operation on itself, so that each element is associated with every other element, like this:
[1 2 3;
4 2 3;
1 5 3;
4 5 3;
1 2 6;
4 2 6;
1 5 6;
4 5 6]
Can someone please help me/point me in right direction?

Réponse acceptée

Sean de Wolski
Sean de Wolski le 10 Avr 2012
A = [1 2 3; 4 5 6]; %data
[xx yy zz] = meshgrid(1:2,3:4,5:6); %column-wise linear indices
B = [A(xx(:)) A(yy(:)) A(zz(:))] %extract
More - generalized to n-dimensions and rows:
A = [1 2 3 7; 4 5 6 9]; %data
[nr nc] = size(A);
C = num2cell(reshape(1:(nr*nc),nr,nc),1);
X = cell(nc,1);
[X{1:nc}] = ndgrid(C{:}); %column-wise linear indices
for ii = nc:-1:1
V(:,ii) = A(reshape(X{ii},[],1));
end
  5 commentaires
Sean de Wolski
Sean de Wolski le 10 Avr 2012
see edit
Vinay Pandey
Vinay Pandey le 10 Avr 2012
Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by