I need create a function for order pairs

1 vue (au cours des 30 derniers jours)
Juan Pérez Álvarez
Juan Pérez Álvarez le 10 Fév 2022
Réponse apportée : Rik le 10 Fév 2022
Hi I nedd create a function for do something like this (using for loops)
A = [1,2,3];
B = [4,5,6];
LA = length(A);
LB = length(B);
LT = LA*LB;
% The matrix of pair orders always be (n,2), in this case C(LT,2) = C(9,2)
C = zeros(LT,2);
C = [(1,4) ; (1,5) ; (1,6) ; (2,4) ; (2,5) ; (2,6) ; (3,4) ; (3,5) ; (3,6)];
I try using this kind of code:
C = []:
for i =1:LT
C(i) = B(A);
% or
C(i) = B[];
end
Any idea?

Réponses (1)

Rik
Rik le 10 Fév 2022
You can create indices with meshgrid or ndgrid, no loop needed.
[indA,indB]=ndgrid(1:numel(A),1:numel(B));
C=[A(indA(:)).' ; B(indB(:)).'];

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by