필터 지우기
필터 지우기

Adjusting if loop to calculate probability for each column of dynamic array

조회 수: 2 (최근 30일)
Hi, I am trying to adjust this for loop such that for each column of the iteration that is stored in list_val_table it counts the number of non-zero elements in each column and divides it by the total number of elements in that column. For instance if after the first iteration the result is a 5x1 array with the first element 5 and the remaining four elements 0's then the value 1/5 is stored.
if length(o_ue)>0
list_val=val_set(o_ue)+length(o_ue);
list_val_table(1:numel(list_val),it_3)=list_val;
end
  댓글 수: 1
pfb
pfb 2015년 4월 15일
not very clear. Your code forms a vector (or scalar) "list_val" from the vector (or scalar) "o_ue", which has length L.
Then it assigns "list_val" to the first L elements of one column of "list_val_table".
For this to work, I think "o_ue" should be a column of integers greater than one and less than the length of the matrix. Does "o_ue" contain the indices of the nonzero elements? Of "list_val_table" perhaps?
A lot of guesswork here
I fail to see the part with the zeros and ones, and the first element.
Also, where should be 1/5 stored?

댓글을 달려면 로그인하십시오.

채택된 답변

Michael Haderlein
Michael Haderlein 2015년 4월 15일
I cannot find a for loop in your code. Anyway, there's also no need for a loop, this can easily be vectorized.
num_of_zeros=sum(o_ue==0,1); %count the number of zeros in every column
frac_of_zeros=num_of_zeros/size(o_ue,1);
  댓글 수: 3
ajk1
ajk1 2015년 4월 15일
Thank you, yes I wanted help with the fraction of nonzero elements but from Michael's response I have adjusted it to work with the rest of my program. Thank you for both of your help.
Michael Haderlein
Michael Haderlein 2015년 4월 16일
Oops, looks like I haven't read your question too carefully. Anyway, good you could get the correct solution though. @pfb: You're right, the direction is not necessary, but I usually give it explicitly when applied on arrays of dimension > 1. It's just my preference.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by