how do i remove this error? Function definitions are not permitted in this context.

조회 수: 1 (최근 30일)
xyRange=[1,5]; %// Starting xy range of particles
numP=3; %// Number of particles generated each day
vx=0.6; vy=0.4; %// x and y velocity
X=[]; Y=[]; %// Vectors start out empty
for day=1:10
%// Generate 3 particles and add to end of vectors X and Y
X=[X;randi(xyRange,numP,1)];
Y=[Y;randi(xyRange,numP,1)];
%// Move all the particles
X=X+vx;
Y=Y+vy;
end
plot(X,Y,'kd');
grid on ;
axis([1,50,1,50]);
function [box] = coord(X,Y)
for j = floor(X/5)+1;
k = floor(Y/5);
box = k*10+j;
end
end

채택된 답변

Thorsten
Thorsten 2015년 6월 29일
You can write it w/o a function, and you don't need the inner for j= ... loop:
for day=1:10
%// Generate 3 particles and add to end of vectors X and Y
X=[X;randi(xyRange,numP,1)];
Y=[Y;randi(xyRange,numP,1)];
%// Move all the particles
X=X+vx;
Y=Y+vy;
end
plot(X,Y,'kd');
grid on ;
axis([1,50,1,50]);
j = floor(X/5)+1;
k = floor(Y/5);
box = k*10+j;
end
  댓글 수: 2
sneha kriplani
sneha kriplani 2015년 6월 29일
hey thanks for ur answer, it helped me sorting out that problem but i was just wondering if u can help me ahead of it also. if u will run this code then it will plot number of particles now i want to count number of particles in each grid box will give u which particle is in which grid but how to add number of particles in each gridbox ??? Thanks in advance
Adam
Adam 2015년 6월 29일
You should give completely new questions their own thread really rather than tagging them onto a comment of an answer.

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

추가 답변 (1개)

James Tursa
James Tursa 2015년 6월 29일
You have your sub function "coord" defined inside your for loop. Move it outside your for loop.
  댓글 수: 2
Adam
Adam 2015년 6월 29일
also if that is a script you need to turn the whole thing into a function in order to define subfunctions.
sneha kriplani
sneha kriplani 2015년 6월 29일
but i want to take X,Y as input if i put it in the starting will it take X,Y as input ??

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by