eg: bb =
0 1.2106 1.0797
-1.2106 0 0.3460
-1.0797 -0.3458 0
>> mean(bb,2)
ans =
0.7634
-0.2882
-0.4752
But I want mean to be:
1.1452
-0.4323
-0.7127
I have a cludgy do-loop that goes through each row, removes the
diagonal '0'. Very slow on big matrices. Is there an easier way?
TIA.
m
Do you know the diagonals are zero?
If so, then why remove them?
sum(bb,2)/(size(bb,2)-1)
HTH,
John
bb = [ 0 1.2106 1.0797 ; -1.2106 0 0.3460 ; -1.0797 -0.3458 0 ]
sizebb = size(bb)
sumbb = sum(bb')'
meansWithNoZerosInbb = sumbb ./ (sizebb(1) - 1)
Basically, you just divide the sum of each row by the number of
(dimensions - 1) since you know that one of the elements will be zero
and should be excluded from calculating the average. I think it
should be very fast and efficient. If you want ALL zeros excluded no
matter where they are located and how many of them there are, you'll
have to use the find() function and it gets more complicated (but
still just a few lines).
Regards,
ImageAnalyst
=============================================
Thanks. As I replied to John, lost sight of what I was trying to do.
Should go to bed now :)
And the answer is ... 'cause I got so caught up in trying to find a
fancy concatenation that I lost sight of what I was trying to do! V.
embarrassing! Thanks.
this is an answer:
replace the zero by nans and use nanmean (in the stats toolbox):
bb = rand(5);
n = size(bb,1);
bb(sub2ind([n,n],1:n,1:n))=nan;
nanmean(bb)
Well, thank god I've never done that.
Said by the fellow who once spent
2-3 hours of time obsessively
optimizing a code that would almost
never be used. Worse, the code took
approximately .01 seconds to run,
and I was able to cut that in half
for my efforts.
8-))
John
one of the very many other solutions (in particular, if you want to
do several operations on the de-diagonalized mat)
m=magic(5);
r=reshape(m(~eye(size(m,2))),size(m)-[1,0]);
m
r
% now, do you op(s), eg
[sum(r);median(r);mean(r);std(r)]
us