Subscript indices must either be real positive integers or logicals.

1 次查看(过去 30 天)
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')

采纳的回答

Star Strider
Star Strider 2015-3-20
编辑:Star Strider 2015-3-20
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 个评论
Star Strider
Star Strider 2015-4-30
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.

请先登录,再进行评论。

更多回答(1 个)

Roger Stafford
Roger Stafford 2015-3-20
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by