How to extract values from a multi column data within a for loop?
2 ビュー (過去 30 日間)
古いコメントを表示
I have a 256 x 4 matrix data. I want to use the values from fourth column in a for loop conditioned on the specific values of first three column cell values.
Each of the first three columns has different integers from 0 to 3. The last column calculates the probability of each of the permutations. I am running 4 for loop statements. How do I get the corresponding probability value from the matrix based on the corresponding combination of integers taken from the first three columns?
0 件のコメント
採用された回答
Udit Gupta
2014 年 5 月 29 日
Let's say your matrix is A Use this code to combine the permutations in the first three columns into a vector
B = [100,10,1];
C = A(:,1:3)*B';
This will give you a vector with the order of the numbers accounted for. You can use the Matlab "unique" function to get a list of all the combinations in the vector C.
Now array indexing can be used to make your calculations. For example if one of the combinations [3,1,0] occurs 4 times and you need to sum the numbers in the 4th column (that was not clear to me) you can calculate that like -
sum(A(C==310,4))
You can do this in one loop by looping over the values in vector C.
1 件のコメント
Udit Gupta
2014 年 5 月 29 日
You can use a similar method. Use B to transform the Phi matrix outside the loop. You can apply a similar transformation based upon the values of l1,l2,---lk and look up the value from the transformed matrix you saved earlier.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!