new array using average row of the old array

I have an array M(1000,1000) and i want to build a new array N(1000,1000), where each element of N has the old value minus the average of its row (in M). If the element is zero no action will be taken. Also, from the average the zeros should be excluded.
Can you help me?
thank you in advance

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
編集済み: Azzi Abdelmalek 2013 年 10 月 13 日

1 投票

Edit
out=bsxfun(@minus,M,mean(M,2)).*not(~M)
%or
idx=M==0;
out=bsxfun(@minus,M,mean(M,2));
out(idx)=0

6 件のコメント

babis
babis 2013 年 10 月 13 日
thank you for your answer but with this commands the average of the row also counts the elements with zero value and i want them to be excluded.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
Do you want to exclude all the rows with zeros
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
If you want to exclude all the rows with zeros
idx=any(~M,2)
out=bsxfun(@minus,M,mean(M,2))
out(idx,:)=M(idx,:)
babis
babis 2013 年 10 月 13 日
dear azzi i want to exclude the zeros when calculating the average. for example in a row like [1 4 7 3 0 0 2 1] the average will be 18/6=3 not 18/8=2.25 thank you again
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
編集済み: Azzi Abdelmalek 2013 年 10 月 13 日
Ok try this
idx=M==0;
moyenne=sum(M,2)./sum(not(~M),2)
out=bsxfun(@minus,M,moyenne)
out(idx)=0
babis
babis 2013 年 10 月 13 日
thanks again this will do it

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 13 日

0 投票

ii = M == 0;
out = bsxfun(@minus,M,sum(M,2)./sum(~ii,2));
out(ii) = 0;

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2013 年 10 月 13 日

コメント済み:

2013 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by