Matsu Thornton
Matsu Thornton

Reputation: 31

MATLAB var(A) and mean(A) error...Subscript indices must either be real positive integers or logicals

I am sure that I have used this function before and it worked perfectly, however I am baffled by what is happening. I can't even run the example code from mathworks.com.

Variance of Matrix
Create a matrix and compute its variance.
A = [4 -7 3; 1 4 -2; 10 7 9];
var(A)
Copyright 2015 The MathWorks, Inc.

Gives me:

Subscript indices must either be real positive integers or logicals.

This is my particular code...it just gives errors on the mean and variance calculations:

%2a
% Create distribution objects with different parameters
pd1 = makedist('Uniform','lower',-2,'upper',3)
% Compute the pdfs
x = -3:0.1:4;
pdf1 = pdf(pd1,x);
% Plot the pdfs
subplot(2,2,1);
plot(x,pdf1)
title('pdf of uniform RV [-2,3]');

ylim([0 0.3]);
% Compute the cdfs
x = -3:.01:4;
cdf1 = cdf(pd1,x);
% Plot the cdfs
subplot (2,2,2);
plot(x,cdf1)
title('CDF of uniform RV[-2,3]');

ylim([0 1.1]);

up = 3
low = -2
mean = (1/2)*(low + up)
var = (1/12)*(up-low)^2


% Do an actual numeric simulation
N=100000;
A = random(pd1,N,1);
[count edges] = histcounts(A,100);
count = [0 count 0];
edges = [-2.1 edges 3];
subplot(2,2,3)
plot(edges(2:end),count/N)
axis([-4 4 0 .02])

CDF = cumsum(count/N);
subplot(2,2,4)
plot(edges(2:end),CDF)
axis([-4 4 0 1.2])

m1 = sum(A)/N
v1 = var(A,0,1)

% Create distribution objects with different parameters
pd2 = makedist('Normal','mu',2,'sigma',sqrt(2.25))
% Compute the pdfs
x = -3:0.1:7;
pdf1 = pdf(pd2,x);
% Plot the pdfs
subplot(2,2,1);
plot(x,pdf1)
title('pdf');
axis([-4 7 0 .05])
ylim([0 0.3]);
% Compute the cdfs
x = -3:.01:4;
cdf1 = cdf(pd2,x);
% Plot the cdfs
subplot (2,2,2);
plot(x,cdf1)
title('CDF');
axis([-4 7 0 1.2])
%ylim([0 1.1]);

up = 3
low = -2
mean = (1/2)*(low + up)
var = (1/12)*(up-low)^2


% Do an actual numeric simulation
N=100000;
A = random(pd2,N,1);
[count edges] = histcounts(A,100);
count = [0 count 0];
edges = [-2.1 edges 3];
subplot(2,2,3)
plot(edges(2:end),count/N)
axis([-4 7 0 .05])

CDF = cumsum(count/N);
subplot(2,2,4)
plot(edges(2:end),CDF)
axis([-4 7 0 1.2])

m2 = mean(A)
v2 = var(A)

Upvotes: 0

Views: 487

Answers (1)

paisanco
paisanco

Reputation: 4164

You've defined variables with the same names as the Matlab mean() and var() functions.

mean = (1/2)*(low + up)
var = (1/12)*(up-low)^2

This is going to cause Matlab to interpret the subsequent attempt to call Matlab's mean() and var() functions, as an attempt to index your user defined variables with the matrix A, which will lead to the error message you see:

Subscript indices must either be real positive integers or logicals

The solution is to rename your user defined variables something other than the Matlab mean and var functions (or any other reserved function name in Matlab) to ensure that your variable names do not clash with Matlab function names. Use Matlab's which to check a possible variable name for a conflict if you are not sure.

Something like

my_mean = (1/2)*(low + up)
my_var = (1/12)*(up-low)^2

Upvotes: 2

Related Questions