How to extract values from a multi column data within a for loop?

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?

 采纳的回答

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 个评论

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 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by