hi, if i have a 2 variables a,b which contains lets say IDs, which function would help do this : a= 1 2 3 b= 4 5 i want to get a variable that contains :
answer= 1 4,1 5,2 4,2 5,3 4,3 5
meaning all the combinations of the values of the two variables ? I tried doing it with two loops but it takes a lot of time (hours) because i have big files, i just started using matlab ,so it may seem a simple question , but i have spent a lot of time trying to do so .

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 31 Jul 2014
Edited: Azzi Abdelmalek on 31 Jul 2014
a= [1 2 3]
b= [4 5]
[x,y]=meshgrid(a,b)
out=[x(:) y(:)]

2 Comments

saeed
saeed on 31 Jul 2014
Edited: saeed on 31 Jul 2014
first of all thanks ,it does the work ,but i have the IDs as texts and that causes a problem , i tried using cellstr(y) y-the variable containing the IDs, then cell2mat() ,ngrid ... but i get a missed up output, why ?
a= {'1' '2' '3'}
b= {'4' '5'}
[ii,jj]=meshgrid(1:numel(a),1:numel(b))
out=[a(ii(:))' b(jj(:))']

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!