Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Aero
Aero le 29 Mai 2014
Commenté : Udit Gupta le 29 Mai 2014
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?

Réponse acceptée

Udit Gupta
Udit Gupta le 29 Mai 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 commentaire
Udit Gupta
Udit Gupta le 29 Mai 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by