Remove empty cells and values for correlations

Hi,
I have a 32x2 cell and I would like to do a correlation for the values in the first column with the values in the second column. However, there are blank cells in the first column. Could anyone help me find a way to do a correlation that excludes all the blank cells and their corresponding values in the second column?
Thanks!

댓글 수: 2

"Blank cells" is not quite specific enough. Could you post a very small example (maybe 4x2 or so) that illustrates your input cell array? For example, do you mean
C = cell(4,2);
C{1,1} = 3;
C{1,2} = 4;
C{2,2} = 4;
C{3,1} = 6;
C{3,2} = 5;
C{4,1} = 4.1;
C{4,2} = 6.2;
where the 2nd row of the first column is an empty cell?
Or do you mean something else by "blank"?
Thang  Le
Thang Le 2014년 6월 17일
편집: Thang Le 2014년 6월 17일
I'm sorry for not being specific. An example would be:
[45] [20]
[16] [32]
[] [10]
[17] [6]
So (3,1) is an empty cell. So this is the same as your example, I think.

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

 채택된 답변

Star Strider
Star Strider 2014년 6월 17일

1 개 추천

Does this do what you want?
CData = {45 20; 16 32; [] 10; 17 6};
CE = cellfun(@isempty, CData(:,1));
Data = cell2mat(CData(~CE,:));
[R, P] = corrcoef(Data)

댓글 수: 6

Thang  Le
Thang Le 2014년 6월 17일
Thank you Star, Strider! This is indeed what I was looking for.
My pleasure!
Thang  Le
Thang Le 2014년 6월 20일
Hi Star Strider, slight adjustment to my question: what if either column or both columns have [] (as opposed to just the first colmn as in my original question). How would I change the code to account for those possibilities? I've tried several things but usually got the Index exceeds matrix dimensions error.
It seems that none of the logical indexing that I find so useful with double arrays works with cell arrays (or at least it doesn’t work for me with cell arrays). Finding all the rows with a missing value was easy enough (my ‘CE’ variable). I finally resorted to a loop to collect only the data with complete rows:
CData = {45 20; 16 32; [] 10; 17 6; 25 []};
CE = cellfun(@or, CData(:,1), CData(:,2), 'Uni',0);
k2 = 0;
for k1 = 1:size(CData,1)
if ~isempty(CE{k1})
k2 = k2+1;
CD(k2,:) = CData(k1,:);
end
end
[R, P] = corrcoef(cell2mat(CD))
It works, but I’m sure there’s a more efficient way to do it.
Thang  Le
Thang Le 2014년 6월 23일
Thank you again, Star Strider!
My pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2014년 6월 17일

댓글:

2014년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by