# Saving output of for loop in array

1 view (last 30 days)
Raphael Williams on 28 Sep 2018
Commented: Aquatris on 28 Sep 2018
Hi, I am trying to save the output of my for loop in an array. Each output will consist of permutations of a previously defined array. I need to save the new array as consisting of my output in a single array.
The code is
For i= eye(10)
End
Now I'm trying to save the output in a new matrix.
Trying
Pnew(i,:)=P
Returns an error "index in position I is invalid"
Kindly help

Aquatris on 28 Sep 2018
Edited: Aquatris on 28 Sep 2018
There are some mistakes and unclear parts in your code. However, using your code, you can save it as;
for i= 1:10
end
Unclear part: why are you calling "randperm" function with "length(Pload)" why are you calling "for i = eye(10)"

Raphael Williams on 28 Sep 2018
Thanks for the response. The objective is to vary the values of the variable Pload 10 times while keeping the initial array structure of Pload intact. Hence since Pload is 1×4, and varied 10 times. I intend on storing the output as 1×40. I want to randomly change the position of each element after a loop, but I want the values to remain unchanged. Defining a solution space but rotating the position
Aquatris on 28 Sep 2018
So since you want to convert the 1x4 to 1x40, you should do something like ;
for i= 1:10
end
Since the Pnew are possible solutions, it might be more convenient to store it as 10x4 matrix instead, in which case you can use below code;
for j =1:10;
end

Stephen Cobeldick on 28 Sep 2018
Edited: Stephen Cobeldick on 28 Sep 2018
for k = 1:10
P(k,:) = P(k,randperm(N));
end
P = reshape(P.',1,[])