I need create a function for order pairs

3 Ansichten (letzte 30 Tage)
Juan Pérez Álvarez
Juan Pérez Álvarez am 10 Feb. 2022
Beantwortet: Rik am 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?

Antworten (1)

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by