mean of non zero elements - why are these two attempt returning different results?

jajdoo

I am attempting to return the mean of non zero elements in each column in my matrix;

my first attempt was:

i = 1:peopleCount;
nonZero1(i,:) = mean(nonzeros(Y(:,i)));

that returns the same result for all columns, while my second attempt:

for i = 1:peopleCount;
    nonZero2(i) = mean(nonzeros(Y(:,i)));
end

returns the correct answer.

They look identical to me, but obviously, they are not.

  1. what went wrong in my first attempt?
  2. how can i make the first version work?
rozsasarpi

As you pointed out the nonzeros gives back a column vector, so you cannot expect multiple means, the same value is assigned to every element of nonZero1(:,i).

A possible solution without loop:

A        = Y(:,i);
A(A==0)  = NaN;
nonZero3 = nanmean(A,1);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why is Scrapy returning duplicate results?

분류에서Dev

Every two elements mean between these two element

분류에서Dev

How to replace non-zero elements randomly with zero?

분류에서Dev

synchronized hover of two different elements

분류에서Dev

Why is AES function returning different value?

분류에서Dev

Perform numpy product over non-zero elements of a row

분류에서Dev

MySQL Random Select Query with limit returning different number of results (undesired)

분류에서Dev

How to combine the results of two different queries with mongoose?

분류에서Dev

Using two different templates for search results - Haystack

분류에서Dev

Why there are two users showing in uptime command results?

분류에서Dev

variable is returning two values, how to store them in different variable?

분류에서Dev

Why are print_r and return returning different values?

분류에서Dev

Why two different methods slice() & substring()?

분류에서Dev

Why are these two Python unicode strings different?

분류에서Dev

Page not returning results

분류에서Dev

What is the difference between these two type annotation and why they are different?

분류에서Dev

Why MySQL BIT_OR() is returning different value than PHP bitwise operation

분류에서Dev

Why MySQL BIT_OR() is returning different value than PHP bitwise operation

분류에서Dev

WorksheetFunction.Sum returning zero for numbers

분류에서Dev

What causes different search results for same elastic search query on two nodes

분류에서Dev

Why is this not returning an @@ERROR?

분류에서Dev

Why is my constructed formula giving different results than the same formula as UDF in Excel?

분류에서Dev

Different results for same numbers

분류에서Dev

what does returning -1 mean in a pseudocode

분류에서Dev

Rectangle custom object shows zero width, should be non-zero

분류에서Dev

MySQL Full Text Search Returning 0 Results

분류에서Dev

Creating UTC time in momentjs is returning weird results

분류에서Dev

mysqli queries not returning results inside function

분류에서Dev

scraper only returning results for first 2 inputs

Related 관련 기사

  1. 1

    Why is Scrapy returning duplicate results?

  2. 2

    Every two elements mean between these two element

  3. 3

    How to replace non-zero elements randomly with zero?

  4. 4

    synchronized hover of two different elements

  5. 5

    Why is AES function returning different value?

  6. 6

    Perform numpy product over non-zero elements of a row

  7. 7

    MySQL Random Select Query with limit returning different number of results (undesired)

  8. 8

    How to combine the results of two different queries with mongoose?

  9. 9

    Using two different templates for search results - Haystack

  10. 10

    Why there are two users showing in uptime command results?

  11. 11

    variable is returning two values, how to store them in different variable?

  12. 12

    Why are print_r and return returning different values?

  13. 13

    Why two different methods slice() & substring()?

  14. 14

    Why are these two Python unicode strings different?

  15. 15

    Page not returning results

  16. 16

    What is the difference between these two type annotation and why they are different?

  17. 17

    Why MySQL BIT_OR() is returning different value than PHP bitwise operation

  18. 18

    Why MySQL BIT_OR() is returning different value than PHP bitwise operation

  19. 19

    WorksheetFunction.Sum returning zero for numbers

  20. 20

    What causes different search results for same elastic search query on two nodes

  21. 21

    Why is this not returning an @@ERROR?

  22. 22

    Why is my constructed formula giving different results than the same formula as UDF in Excel?

  23. 23

    Different results for same numbers

  24. 24

    what does returning -1 mean in a pseudocode

  25. 25

    Rectangle custom object shows zero width, should be non-zero

  26. 26

    MySQL Full Text Search Returning 0 Results

  27. 27

    Creating UTC time in momentjs is returning weird results

  28. 28

    mysqli queries not returning results inside function

  29. 29

    scraper only returning results for first 2 inputs

뜨겁다태그

보관