Matrix manipulations

Hey all,
I'm having a bit of trouble calculating conditional proabbilities, say I have a matrix such as:
CgvB =
0 0
0 0
0 0
0 1
0 1
0 1
0 1
0 1
0 1
0 1
1 0
1 0
1 1
1 1
and I want to calculate the probability of having a 1 on the 2nd column given having a 1 on the first column in this case ists 2/14, but I'm using a script to automatically calculate all these probabilties. This is what I've started with: p=CgvB(length(CgvB(:,:)==1))/length(CgvB); this works but for empty matrices i get the error :Subscript indices must either be real positive integers or logicals. How can I perform these calculations without errors? Thanks for any help.

 채택된 답변

Paulo Silva
Paulo Silva 2011년 6월 5일

0 개 추천

You can ensure that your array has the proper dimensions before doing the calculation
[sr,sc]=size(CgvB);
if (sr>0 & sc==2)
p=CgvB(length(CgvB(:,:)==1))/length(CgvB);
else
disp('The array must have at least one row and two columns');
end

댓글 수: 2

Ebrahim
Ebrahim 2011년 6월 5일
Thanks for that. Can I also just say after else, that p = []? This is because I don't want to display anything but just continue with my calculations.
Paulo Silva
Paulo Silva 2011년 6월 5일
yes you can give p some value when the arrays doesn't have the proper dimensions, it can also be done at the start of the code, give p a default value, you can use Inf or NaN instead of []

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by