How to create multiple matrixes from a single for loop?

Hi, I have a for loop that produces a matrix after each iteration and currently the resultant matrix is a combination of the matrixes from each iteration.
However, I want to be able to have separate matrixes for each iteration so that I am able to index the first and last row of each.
Heres the part of the script:
% Plotting gradient descent
for counter = 1:length(startingcoordinates)
% Extracting the x and y from the starting coordinates
xi = startingcoordinates(counter,1);
yi = startingcoordinates(counter,2);
% Storing each trajectory
Trajectory = gradient_descent(Z,xi,yi,gamma,tau);
end
Where:
startingcoordinates is a 4 by 2 matrix containing x and y co-ordinates.
gradient_descent is a function that finds the minimum of a surface Z.
Thanks for reading.

2 件のコメント

Jan
Jan 2019 年 1 月 11 日
I've formatted your message and removed many blank lines to improve the readability.
"the resultant matrix is a combination of the matrixes from each iteration." - This is not clear. How is it "combinated"?
Carlos  Pedro
Carlos Pedro 2019 年 1 月 11 日
Hi Jan, thanks.
The output is currently the output of one iteration followed by another iteration. Its probably worth saying that each iteration produces a large matrix of size n by 3.

サインインしてコメントする。

 採用された回答

Jan
Jan 2019 年 1 月 11 日

0 投票

With some guessing:
nCoor = size(startingcoordinates, 1); % Safer than LENGTH()!!!
Trajectory = cell(1, nCoor);
% Plotting gradient descent
for counter = 1:nCoor
% Extracting the x and y from the starting coordinates
xi = startingcoordinates(counter,1);
yi = startingcoordinates(counter,2);
% Storing each trajectory
Trajectory{counter} = gradient_descent(Z,xi,yi,gamma,tau);
end

1 件のコメント

Carlos  Pedro
Carlos Pedro 2019 年 1 月 11 日
Thanks Jen, thats just what I was looking for!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2019 年 1 月 11 日

コメント済み:

2019 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by