How can I skip zero values in an array when evaluating the correlation coefficient ?

Hello, I am kind of new to Matlab and I am running into some issues evaluating the correlation coefficient between the natural logarithm of two arrays(chan1 and chan2) each of size 577*1 that includes zero values that I would like to skip when calculating the correlation coefficient. I thought I could do a nested for loop to skip the zero values and this is what I have:
for chan1 ~= 0
for chan2 ~=0
c = corrcoef(log(chan1),log(chan2))
end
end
But it seems like that Matlab does not like it when one uses does not equal to in a for loop
Please help me solving this issue.
Thanks!

댓글 수: 1

Another thing I thought of doing is:
if chan1 == 0
return
elseif chan2 == 0
return
else c = corrcoef(log(chan1),log(chan2))
end
but this is what I get:
c =
NaN NaN
NaN NaN

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

 채택된 답변

% Some pretend data
chan1 = round(randn(1000,1));
chan2 = round(randn(1000,1));
% Identify pairs where both are nonzero
idxBothNonzero = chan1 > 0 & chan2 > 0;
% Evaluate correlation, using only pairs where both are nonzero
c = corrcoef(log(chan1(idxBothNonzero)),log(chan2(idxBothNonzero)))

댓글 수: 2

It works, Thank you very much!!
Hi, can you suggest a solution for a matrix with several "0" cells, which should be ignored for corr?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 7월 14일

댓글:

2018년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by