필터 지우기
필터 지우기

Count results of for-if/else-statements and write it in matrix

조회 수: 3 (최근 30일)
Markus Vietze
Markus Vietze 2021년 7월 13일
답변: VINAYAK LUHA 2023년 10월 13일
Hello!
I want to store in my code the results of the for loop (line 140-166), how often which statement("<a", ">=a & <b", ....) was used, in a matrix. I have tried to record this numerically (1-4) for the value "Liste2". But now I have the problem that I don't know how to do this for all 722 iterations of the big for loop, so that at the end I get one column of a matrix for each of the 722 iterations. Also, unfortunately, the lengths of the arrays vary.
Thanks for the help!

답변 (1개)

VINAYAK LUHA
VINAYAK LUHA 2023년 10월 13일
Hi Markus,
I understand that you have a loop in your code that contains multiple "if-else if...else" conditional statements. Your objective is to keep a log of which conditional statement is triggered in each iteration of the loop.
Here is a solution to achieve the above use case using a 2D matrix as a data structure to log information:
  1. Create a 2D zeros matrix of size (m,n), where “m” and “n” corresponds to the number of loop iterations and number of conditional statements inside the loop respectively.
  2. Since the conditional statements are mutually exclusive, in any iteration there is only one condition which evaluates to “true. Acknowledge this event by marking “1” in the data structure.
  3. For any iteration “n”:
  • Store the count forcondition 1” my marking 1 in the nth row and 1st column.
  • Store the count for condition 2” my marking 1 in the nth row and 2nd column and so on...
As a suggestion, if you are memory constrained, you may optimize the approach using bit manipulation techniques to store the log in a 1D array of size (m,1) instead of 2D matrix of size (m,n).
I hope you find the provided solution useful, and this helps you keep a log of which conditional statement is triggered in each iteration of the loop.
Regards,
Vinayak Luha

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by