Subscript indices must either be real positive integers or logicals.
1 view (last 30 days)
Show older comments
Katarzyna Wieciorek
on 20 Mar 2015
Commented: Star Strider
on 30 Apr 2015
Hi,
I see I am really bad at indexing so if someone could tell me some general rules (but less general than in online tutorials) I would be grateful.
*Subscript indices must either be real positive integers or logicals.
Error in PL2ex1 (line 4) meandata = mean(nonoise.data_out);*
File to read was uploaded.
%PL2ex1
nonoise = load('verg1');
meandata = mean(nonoise.data_out);
trials = repmat(meandata, 1000, 1);
%add noise with normal distribution (randn) and repeat
sigma = randn(1);
for i = 1:1000
trials(i) = trials(i) + randn(1)*randn(size(trials(i)));
end
%compare plots and mean of trials
x = linspace(0, 10); % define axis x
figure
subplot(3,3,1)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,2)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,3)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,4)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,5)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,6)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,7)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,8)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
%mean
subplot(3,3,9)
means = zeros(1,400);
for i = 1:400
means(1,i) = mean(trials(:,i));
end
plot(x, means)
title('Means')
0 Comments
Accepted Answer
Star Strider
on 20 Mar 2015
Edited: Star Strider
on 20 Mar 2015
In the Command Window, type:
whos mean
The output should be nothing (just the >> cursor).
If you get something like this:
Name Size Bytes Class Attributes
mean 1x1 8 double
that means you have assigned a variable to ‘mean’, ‘overshadowing’ the mean function.
Solution: Rename the variable.
11 Comments
Star Strider
on 30 Apr 2015
See if this slight modification of your code — that does all your first 8 plots in the for loop — improves it:
nonoise = load('verg1');
meandata = mean(nonoise.data_out);
trials = repmat(meandata, 1000, 1);
x = linspace(0, 10, 400); % define axis x
trials = trials + randn(size(trials))*0.5;
for k1 = 1:8
subplot(3,3,k1)
n = randi(1000);
plot(x, trials(n,:))
title(sprintf('Trial %d', n))
end
%mean
subplot(3,3,9)
means = mean(trials);
plot(x, means)
title('Means')
Experiment with different constants (actually, the standard deviation, here 0.5) to get the randomness you want. you will see that the plots are now all different. I believe the noise you were adding was too small to be visible on your plots.
More Answers (1)
Roger Stafford
on 20 Mar 2015
I would guess that somewhere in your system you have defined a variable named 'mean', in which case matlab misinterprets the calls to the function 'mean' as referring to that variable. That is the only way I can account for the error message. You should never use the names of matlab functions as variables for just this reason.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!