index out of bounds because numel(x)=1

3 ビュー (過去 30 日間)
wen
wen 2014 年 10 月 26 日
回答済み: Image Analyst 2014 年 10 月 26 日
Hi guys, I am writing a simple code to calculate probability of a two dimension t random number distribution with the Monte-Caro method as you can see. While there is error "Attempted to access x(2); index out of bounds because numel(x)=1." Many thanks for your help!
% MC estimate of probability
clear;clc;
n=10000;
x=trnd(6)*1+3;
y=trnd(6)*1+3;
%figure(1);
%plot(x,y,'r+')
c=0;s=0;
for i=1:n
s=s+1;
if 2<x(i)&&x(i)<4&&y(i)>4||x(i)>4&&y(i)>2 % inside the shadow
c=c+1;
% figure(2);
% plot(x(i),y(i),'b+');
% hold on;
else % outside the shadow
% figure(2);
% plot(x(i),y(i),'r+');
end
end
p=c/s;
Attempted to access x(2); index out of bounds because numel(x)=1.
Error in normal (line 13)
if 2<x(i)&&x(i)<4&&y(i)>4||x(i)>4&&y(i)>2 % inside the shadow

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 26 日
Is x a 1D or 2D array - I don't have the stats toolbox so that's why I asked. With rand(), x would have been 2D. Anyway, try
for i=1 : size(x, 1)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by