mean function gives an error but not median

조회 수: 7 (최근 30일)
Yoon
Yoon 2025년 1월 9일
댓글: Star Strider 2025년 1월 9일
I am trying to calculate mean, median and 90% percentile of arrays. Median and prctile work fine but mean gives an error message:
Array indices must be positive integers or logical values.
Teh error comes from Averted_Deaths_Array(ij,1) = mean(Averted_Deaths(ij,:)); in the following code.
The code is simple.
TotalDeathsArray = rand(12,2500);
% Calculate Averted cases
for i=1:2500
%2: PCV13 1+1, 3: PCV15 2+1, 4: PCV15 1+1
% 1+1
Averted_Deaths(1,i) = TotalDeathsArray(2,i) - TotalDeathsArray(4,i);
Averted_Deaths(2,i) = TotalDeathsArray(6,i) - TotalDeathsArray(8,i);
Averted_Deaths(3,i) = TotalDeathsArray(10,i) - TotalDeathsArray(12,i);
% 2+1
Averted_Deaths(4,i) = TotalDeathsArray(2,i) - TotalDeathsArray(3,i);
Averted_Deaths(5,i) = TotalDeathsArray(6,i) - TotalDeathsArray(7,i);
Averted_Deaths(6,i) = TotalDeathsArray(10,i) - TotalDeathsArray(11,i);
end
Averted_Deaths_Array = zeros(6,3);
for ij=1:6 % Averted QALY
%Averted QALY when changing with New 1+1
Averted_Deaths_Array(ij,1) = mean(Averted_Deaths(ij,:));
Averted_Deaths_Array(ij,2) = median(Averted_Deaths(ij,:));
Averted_Deaths_Array(ij,3) = prctile(Averted_Deaths(ij,:),90);
end
  댓글 수: 2
Torsten
Torsten 2025년 1월 9일
편집: Torsten 2025년 1월 9일
Works for me (see above).
Maybe you use a variable named "mean" or you have a file named "mean.m" in your working directory. If so, rename the variable/file.
Yoon
Yoon 2025년 1월 9일
Thank you!. When I loaded a mat file, there was an array called 'mean'. It works now.

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

채택된 답변

Star Strider
Star Strider 2025년 1월 9일
That you are getting theee error:
Array indices must be positive integers or logical values.
indicates that somewhere in your code you have a variable named ‘mean’. You need to name it something different that makes sense in the context you are using it.
Run thiis in your Command Window or a script:
which mean -all
to confirm that. If that is true, the first entry in the results should be somethiing like:
mean is a variiable.
.
  댓글 수: 2
Yoon
Yoon 2025년 1월 9일
Thank you!. When I loaded a mat file, there was an array called 'mean'. It works now.
Star Strider
Star Strider 2025년 1월 9일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by