Info

This question is closed. Reopen it to edit or answer.

what's a smarter way to associate numbers in an array to variable names?

1 view (last 30 days)
I've asked this in the comments as a follow up to another question, but I think I broke etiquette because it wasn't related.. I'm reposting it here as a standalone question so hopefully others with similar queries can look at how something like this can be solved.
I’ve done a pretty ugly section of code that requires quite a bit of manual work, which is innocuous with 4 columns of random data, but would be a pain once I roll out the program to real data, of which I have tons. But ugly was all I was able to conjure up.
So, this:
[nRows, nCols] = size( data ) ;
combinations =nchoosek(1:nCols,2);
gives me an array of unique combinations:
1 2
1 3
1 4
2 3
2 4
3 4
In order to associate each number in array combinations to the variable names, where these are contained in array ‘headers’, from:
headers = raw(1,1:4);
I’ve done this:
Nrowscombinations=size(combinations,1);
Ncolumnscombinations=size(combinations,2);
nameVar = strings(Nrowscombinations,Ncolumnscombinations);
for iii=1 : Nrowscombinations
for jjj =1 : Ncolumnscombinations
if combinations(iii,jjj)==1
nameVar(iii,jjj)=headers{1,1};
end
if combinations(iii,jjj)==2
nameVar(iii,jjj)=headers{1,2};
end
if combinations(iii,jjj)==3
nameVar(iii,jjj) = headers{1,3};
end
if combinations(iii,jjj)==4
nameVar(iii,jjj)=headers{1,4};
end
end
end
And nameVar is what I want, a 6x2 array of strings containing:
X Y
X V
X Z
Y V
Y Z
V Z
Which is what I want.
Is there an intelligent way of doing this, one that wouldn’t have me manually insert that co-ordinates and the numbers like that –you can imagine how hideous this becomes with hundreds of variables?

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!