How to extract values from a multi column data within a for loop?
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Udit Gupta
on 29 May 2014
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 Comment
Udit Gupta
on 29 May 2014
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.
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!