找到矩阵中每列数据的均值大于的数值。

huangapple go评论62阅读模式
英文:

Find values greater than the mean of each column of data in a matrix

问题

我有一个包括5列(月份)和4行(年份)的矩阵,表示每月的降水量。如何找到大于每列(月份)平均值的值。应该将每列的值与每列的平均数据进行比较,如果大于平均值,则打印1,否则打印0,以便得到一个具有相同行和列的矩阵,其中包含0或1的值。

这里是随机数据。

w = randi([1 12], 4, 5)

例如

result =
     0     1     0     1     1
     1     0     1     0     1
     1     0     1     1     0
     0     0     1     1     1
英文:

I have a matrix including 5 columns(months) and 4 rows(years) indicating monthly precipitation. How to find values greater than the average of each column (month). The values of each column should be compared with the average data of each column and if it is greater print 1 instead of that value or else print 0 so we have a matrix with the same rows and columns with 0 or 1 value.

here is random data.

w= randi([1 12], 4,5)

w =

 1    12     1     8     9
 9     3    11     3    10
 8     2     8    12     1
 2     1     7     8    10

for example

result=
     0     1     0     1     1
     1     0     1     0     1
     1     0     1     1     0
     0     0     1     1     1

答案1

得分: 3

你只需要

result = w > mean(w, 1)

这利用了隐式扩展。请注意,结果的类型是 logical;如果需要,您可以将其转换为 double

英文:

You only need

result = w > mean(w, 1);

which makes use of implicit expansion. Note that the result is of type logical; if necessary you can convert to double.

答案2

得分: 1

A = randi([1 5],4,6);
[rows, columns] = size(A);
for i = 1:columns
for j = 1:rows
if A(j,i) >= mean(A(:,i))
P(j,i) = 1;
else P(j,i) = 0;
end
end
end

英文:
A = randi([1 5],4,6);
[rows, columns] = size (A);
for i = 1:columns
  for j =1:rows
   if  A(j,i) >= mean(A(:,i));
    P(j,i) = 1;
   else P(j,i)=0;
   end
  end
end

huangapple
  • 本文由 发表于 2023年6月22日 17:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定