All possible combinations of variables
11 次查看(过去 30 天)
显示 更早的评论
Hey everybody,
I have a system with x variables and I want to obtain by using MATLAB a combination of all of them in all the possible groups. Let me introduce you an example:
Lets suppose I have 6 variables for example(1,2,3,4,5,6), it means there will be 63 combinations without repeating any. My aim is to achieve all the combinations like this:
[1] [2] [3] [4] [5] [6]
[1,2] [1,3] [1,4] [1,5] [1,6] [2,3] [2,4] ...
[1,2,3] [1,2,4] [1,2,5] [1,2,6] [1,3,4] [1,3,5] [1,3,6] [1,4,5] [1,4,6] [1,5,6] [2,3,4] ....
and the same for groups of 4, 5 and 6 variables.
Does anybody know how to do it for X number of variables? I need to do this to compute an statistics equation in order to detect errors in the measured values of the system.
Thank you for your attention!
2 个评论
Andrew Reibold
2014-8-5
Do you just need to know 'how many' combinations there are, or do you need all of the combinations output. If you need them output, how should they be displayed or stored?
Josep
2014-8-5
No, I need all of the combinations output. Could I email you in order to be more explicit?
Thank you!
采纳的回答
Star Strider
2014-8-5
编辑:Star Strider
2014-8-5
v = 1:6;
c2 = combnk(v,2)
c3 = combnk(v,3)
... and so on for 4, 5, and 6.
24 个评论
Josep
2014-8-5
I don't have the Statistic Toolbox but it seems useful. Do you know if I can download for free anywhere? If I cannot, do you know an alternative way to do the same?
Star Strider
2014-8-5
It’s not available as a free download to the best of my knowledge. Searching the File Exchange for combinations turned up some user-contributed functions that may work for you.
I’ll also experiment and see what I can come up with.
Josep
2014-8-5
Oh that is a pity!
I attach you here an example of what I exactly need and if you have any idea of how to do it I would be very thankful.
Thank you for your attention Star Strider!
Star Strider
2014-8-5
As for the combinations calculations, I suggest Matt Fig’s File Exchange contribution COMBINATOR - combinations AND permutations. It seems to the the most popular and most highly-regarded, and also seems to be the most robust and versatile.
The Generalized Likelihood Ratio for some reason isn’t in any of my references (at least by that name). I found Lecture 10: The Generalized Likelihood Ratio that I haven’t read but looks good. There are others that might be more applicable to your intended application.
I honestly have no idea how to apply the GLR to your stream flow data, because I don’t understand what you are doing.
Josep
2014-8-6
What I am doing is a data reconciliation of a real paper mill in Canada. I have the measured data from the mill(which will be my inputs, linked from an Excel page), and I need to compute the two equations of the GLR test for all the possible combinations of the variables in the system in order to detect multiple gross errors in the data.
Thank you for you attention!
Star Strider
2014-8-6
My pleasure!
According to Wikipedia, Data validation and reconciliation is at least initially an optimisation problem. You can probably do that with fminsearch, but will need the Optimization Toolbox for the other optimisation functions. There are no MATLAB functions that will do data reconciliation (at least that I could find), although I’m certain you can use existing MATLAB functions to do what you want. I can’t find anything in the File Exchange when I did a search on ‘data reconciliation’.
Since the PDF data you posted likely come from previous studies, I suggest you consult that paper for the procedures it uses. Code them yourself in MATLAB, test your code, preferably using the data it used (if available), and see how the results compare. Use your code on the individual streams first. If that is successful, then combine them according to the established procedures (in the paper you quoted the PDF from), and go from there. I have never needed to do data reconciliation and so have no experience with it, I know essentially nothing about hydrology, and I have no idea how you combine your streamflow data.
If you have problems getting your code to run or give the correct results, return here with a full description of what you want to do, what you did, what you want your code to do, what it did or did not do that you wanted it to, and the full text of any error messages it threw, along with the line that threw the error. It would likely help to attach a PDF of the paper you used as your reference for your code.
Josep
2014-8-7
Very kind for your part Star Strider, it is a pleasure finding someone like you!
The thing is that in this part of gross error detection I do not have to do a data reconciliation. I have only X number of variables in my system but as you could see, first I was trying to prove an example of a book that has 6 variables).
The thing is that I will have stored in an Excel page some data from the variables (the standard deviation, the measured value of the variable..). The procedure of the method that I am using or that I aim to use is based in the GLR test statistics for multiple gross error detection. It consists on doing all the combinations possibles for all the variables without repeating as I said in my first question above. In each combination, I have to compute 2 equations, the test statistics and the Type 1 error probability in order to achieve two values for each combination (see the document attached)
I hope you would understand my problem, thank you so much!
Star Strider
2014-8-7
I don’t understand how your definition of the covariance matrix H. You are correct that the diagonals are the squared values of the standard deviations, but it is not a diagonal matrix. It contains the covariances of the various components of whatever data created it. The cov function can do that for you.
What is r? It must be a (2x1) vector, but how is it calculated?
It will likely not be difficult to calculate your T statistic, but it will be tedious, since it will involve looping over several combinations of variables. You will have to index it in the loop over the variables that are used to create it, so T will be a matrix. The link to the File Exchange function I sent you earlier will generate the combinations for you.
Given Fk, H, and r, the T calculation is straightforward:
T = (Fk'*inv(H)*Fk)\(Fk'*inv(H)*r);
Using the inv function is not considered good programming practice, but I don’t see how to avoid it here.
You would have to index T in a loop to save it for the various combinations of variables you’re using to generate it, but that would not be difficult.
Star Strider
2014-8-8
Josep’s ‘Answer’ moved here:
The problem is not on calculating the test statistics, the thing that is more difficult for me and I don`t know how to do and program is the fact that as you said, it will involves looping over several combinations of variables.
The other thing I don`t know how I will manage to do it is to index T in a loop to save it for the various combinations of variables I am using to generate it.
The r comes from other values that I will calculate when I know exactly how to compute the first part of the equation.
You said it would be easy, but for me, I have no idea on how to program it. I would be so thankful another time if you help me on that, doing the loop, and obtain the test statistics for all the combinations, considering that the number of variables increases and thus the size of the matrices.
Sorry for my lack of capacity on programming and having ideas, this world is a little bit new for me.
Thank you one more time, you are so helpful for me.
Star Strider
2014-8-8
For the situation involving the two-variable combinations, COMBINATOR would return a (15x2) matrix (6 numbers taken 2 at at time). The loop might go like this:
N = 2;
combs = COMBINATOR MATRIX OUTPUT FOR 6 NUMBERS TAKEN N AT A TIME;
for k2 = 1:size(combs,1)
% Calculate data for this combination of (combs(k1,1),combs(k1,2),...) —> |Fk|,|H|,|r|
T(combs(k1,1),combs(k1,2),...) = ... % Calculate T for these data;
end
At the end of the loop, you will have a matrix of T values whose subscripts are the values for the various combinations of variables. The same goes for the three-variable situation, except that instead of (combs(k1,1),combs(k1,2)) you will have (combs(k1,1),combs(k1,2),combs(k1,3)), and so on for the others. You will have to store the T matrix as a ‘.mat’ file after the end of each loop, first renaming it T2 for the two-variable run, T3 for the three-variable run, and so for the rest. (This prevents you from overwriting a generic T matrix if you load all into your workspace at the same time.)
You could do the same thing with nested for loops, but that would quickly get cumbersome with six nested loops. I would use the method I outlined here. You can use one loop for all runs.
Obviously, this is simply an outline of one way you might do it.
Josep
2014-8-8
编辑:Josep
2014-8-8
Okay, but when I put the next code on matlab it returns me an error: combinator(4,2,'c') % Combinations without repetition
As Matt Fig said, by running this code I am supposed to achieve this results(that would be all the combinations I want):
ans = 1 2
1 3
1 4
2 3
2 4
3 4
The problem is that it doesn't work, telling me there is an error but I don't know how to correct it. Do you know what should be the problem?
Star Strider
2014-8-8
I’m reluctant to troubleshoot other people’s code. When I checked the ‘combinator’ site, there is a problem but only with high numbers of combinations, and a fix for it is posted.
What is the error you’re getting?
Also, I notice ‘combinator’ does not allow output arguments. You can easily save the output of combinator, for instance as the variable ‘combs’, by calling combinator then:
combs = ans;
That saves the results in ‘combs’.
Josep
2014-8-8
I put this: combs = combinator(4,2,'c') % Combinations without repetition combs
and matlab returns me this error:
Error in combotrial (line 1) combs = combinator(4,2,'c') % Combinations without repetition
Star Strider
2014-8-8
The function doesn’t allow output arguments.
Please see my previous Comment. I suggest a fix for that.
Josep
2014-8-8
编辑:Star Strider
2014-8-8
combinator(4,2,'c') % Combinations without repetition
combs =ans;
Error message:
>> combs1
Error using combinator
Too many input arguments.
Error in combs1 (line 1)
combinator(4,2,'c') % Combinations without repetition
Star Strider
2014-8-8
Immediately after your combs1 call from the Command Window (even if it produces the error), run the following line from the Command Window:
which combinator -all
You can simply copy that line and paste it to the Command Window. That should only produce one result, the location of combinator in your MATLAB search path. If it produces anything else, it could be that you’re creating another combinator in your workspace somewhere.
Star Strider
2014-8-11
I’d like to get your code to work.
If you’re still getting the same error, have you verified that combinator is in your MATLAB search path, and preferably user path? It looks like you’re calling it correctly, so MATLAB not being able to find it’s the only problem I can see. Download it to your user directory (usually C:\...\Documents\MATLAB\ on Windows systems, or copy it to there if you’ve put it somewhere else, like your Downloads directory where MATLAB won’t look for it), then see if your code works. Be absolutely certain you don’t have another function named combinator in your MATLAB search path.
Matt’s routines are highly respected, well-reviewed, quite reliable, and no other problems like yours have been reported for combinator.
Star Strider
2014-8-12
编辑:Star Strider
2014-8-12
I believe I see the problem.
If you want to use combs.m as a function, you have to create it as a function.
Copy-paste this entire code to your combs.m file, overwriting or replacing everything currently in it:
function combo = combs(n,k)
combinator(n,k,'c'); % Combinations without repetition
combo = ans;
end
Calling it as:
combo = combs(4,2);
should then return your matrix of combinations in your combo variable.
Josep
2014-8-13
I have done this:
function combo = combs(6,2)
combinator(n,k,'c'); % Combinations without repetition
combo = ans;
end
And I still have an error message:
>> combs(6, 2) Error: File: combs.m Line: 2 Column: 24 Unexpected MATLAB expression.
Star Strider
2014-8-13
编辑:Star Strider
2014-8-13
That’s strange. Line 2 is the combinator call, and column 24 is in the comment!
I’ll download combinator in a few minutes to see if I can replicate your error.
Later that same day ...
I downloaded combinator, saved it to my user directory, and ran this code:
combinator(6,2,'c'); % Combinations without repetition
combo = ans;
without problems, returning combo as a (15x2) double matrix. I have no idea what may be the problem with your code, or perhaps with your MATLAB installation.
I still suggest you run this command from the Command Window:
which combinator -all
and copy the results to a comment to this thread.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)