I need create a function for order pairs

1 view (last 30 days)
Juan Pérez Álvarez
Juan Pérez Álvarez on 10 Feb 2022
Answered: Rik on 10 Feb 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?

Answers (1)

Rik
Rik on 10 Feb 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(:)).'];

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!