How to extract values from a multi column data within a for loop?
조회 수: 6 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!