double indexed variable (new user)

조회 수: 3 (최근 30일)
Unknowndruid
Unknowndruid 2012년 3월 6일
I am very new to Matlab. I am trying to create ixj number of variables using the function below. I am getting an error message for pos(i,j) = [b/2+partsize/2+(i-1)*(b+partsize), b/2+partsize/2+(j-1)*(b+partsize)] line. How do I create indexed variables that I can call later in Matlab? Here pos(i,j)==> 1x2 vector (x and y coordinates) e.g I expect to obtain something like the following: pos (1,1)= [ 2 2] pos ( 1,2) = [2 4] pos ( 2,1) = [4 2] pos (2, 2) = [4 4] etc.
function b= posgen(cellsize, particleno, partsize)
partnoline =(particleno)^(1/2);
b= (cellsize-partnoline*partsize)/partnoline;
for i=1:partnoline;
for j=1:partnoline;
pos(i,j) = [b/2+partsize/2+(i-1)*(b+partsize), b/2+partsize/2+(j-1)*(b+partsize)]
end
end
What am I doing wrong? Can you help please
  댓글 수: 1
Unknowndruid
Unknowndruid 2012년 3월 6일
To make it simpler,
for i=1:n
for j=1:m
pos(i,j)=[n^2, m]
end
end
I want to create indexed variables and want to able to recall them their indexes with pos (2,3) or pos (1,5) etc.

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

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 6일
Numeric arrays cannot have multiple elements (e.g., x and y) at a location. Use an additional dimension, or use cell arrays.
pos(i,j,:) = [b/2+partsize/2+(i-1)*(b+partsize), b/2+partsize/2+(j-1)*(b+partsize)];
  댓글 수: 3
Unknowndruid
Unknowndruid 2012년 3월 6일
sorry my bad. It worked!!
But not the way I wanted:(
pos(:,:,1) =
2.5000
pos(:,:,2) =
2.5000
pos(:,:,1) =
2.5000 2.5000
pos(:,:,2) =
2.5000 7.5000
pos(:,:,1) =
2.5000 2.5000
7.5000 0
pos(:,:,2) =
2.5000 7.5000
2.5000 0
pos(:,:,1) =
2.5000 2.5000
7.5000 7.5000
pos(:,:,2) =
2.5000 7.5000
2.5000 7.5000
Unknowndruid
Unknowndruid 2012년 3월 6일
Ok. I think that I am too new to understand the significance of this outcome. I now understand that I can recall pos (1,2,2) etc.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by