Creating a new matrix in each iteration

5 views (last 30 days)
Hey guys,
I am trying to create a new matrix in each iteration.
something like
for i=1:n
somerandommatrix(n)(i,j)=[x,y];
end
anyone know how to do such thing?
Thanks in advance.
  3 Comments
E K
E K on 28 Jul 2012
Sorry if i am not clear enough, what i am trying to do is creating a matrix named coveredrtp for the first possible base site , do the same thing for j=2 (a second possiblebase site which contains (x.y) coordinates than check it with the first one how many elements they share in common. Then for the third possiblebasesite position i want to make this comparision with first and second one.
In a more mathematical explanation
i want to check coveredrtp(i,:) for possiblebasestation(x,y) with every coveredrtp(j,:) where j<i. And here is my loop if it will help;
and sizes of matrix will stay same in columns because it will represent coordinates but it can change in manner of number of rows.
thank you again
if true
for j=1:length(possiblebasesite)
xtest=possiblebasesite(j,1);
ytest=possiblebasesite(j,2);
for i=1:numberofrtp
if (rtpposition(i,1) <= xtest+radius||rtpposition(i,1) <= xtest-radius)...
&& (rtpposition(i,2) <= ytest+radius||rtpposition(i,1) <= ytest-radius)
if sqrt( ((xtest-rtpposition(i,1))^2) + ((ytest-rtpposition(i,2))^2)) <= radius
coveredrtptest(k,1)=rtpposition(i,1);
coveredrtptest(k,2)=rtpposition(i,2);
k=k+1;
end
end
end
end end
per isakson
per isakson on 28 Jul 2012
BTW:
if false
...
...
end
used to be a trick to "comment out" block of code. Now that is better done with
%{
....
....
%}
which Matlab understand and turns the "comments" green. More readable - fewer mistakes.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 28 Jul 2012
Edited: per isakson on 28 Jul 2012
Since we don't know the size of coveredrtptest beforehand it is a bit tricky to preallocate it.
This will give you a cell array, somerandommatrix, the elements, coveredrtptest, of which are double arrays.
len = length(possiblebasesite);
somerandommatrix = cell( len, 1 ); % allocate memory
for jj=1:len
coveredrtptest = [];
for ii=1:numberofrtp
if ...
if ...
coveredrtptest(end+1,1:2)=rtpposition(ii,1:2);
end
end
end
somerandommatrix{jj} = coveredrtptest;
end

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jul 2012

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!