How to I add loop vector iteration values to one big matrix?

2 次查看(过去 30 天)
I have a loop that iterates a function 6 times, each time it produces a matrix. How do I add each iteration to a single matrix in a new column? I should see a 6x6 matrix at the end of the loop.
I would like X matrix to have all 6 rows answers. A single iteration of this function will provide an answer that will look similar to this:
2904.5 3019.3 3065.7 3070.6 3044.3 2996.1
The loop code:
for phi=0.5:0.2:1.5
X=Function(3000,1,phi)
end
The code above just replaces each iteration so I just see one line.
I tried to use something along the lines the code below, but it will not work, or will add everything to one row. (one other function would work if the answer is one variable, but will not work if there are multiple variables in one row)
it = it+1
X(it)=Function(3000,1,phi)
Thanks again!
  4 个评论
Stephen23
Stephen23 2018-10-6
"The output will be a 1x5 matrix"
but in your question you show a 1x6 vector. Which is correct?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-10-6
编辑:Stephen23 2018-10-6
Assuming that myfun outputs a 5-element vector, just use indexing like this:
phi = 0.5:0.2:1.5;
mat = nan(5,numel(phi));
for k = 1:numel(phi)
mat(:,k) = myfun(3000,1,phi(k));
end
This puts each result as one column of mat, which makes plotting easier. Or you could swap the dimensions of mat to put the results as rows. Whatever works for you.
  2 个评论
cancel077
cancel077 2018-10-6
It works for 0.5 to 1.5! thank you. But when I try to make it
phi = 1:1:20;
It spits out the following error:
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.661708e-19.
> In Gibbs (line 49)
In Part_B (line 17)
Warning: Matrix is singular to working precision.
Gibbs is my function (your 'myfun' example). Line 49 is just the output line that provides me the matrix
Part_B (line 47) is this line:
Y2(:,k) = Gibbs(T,1,phi(k))
full code:
phi = 1:1:20;
Y2 = nan(6,numel(phi));
for k = 1:numel(phi)
Y2(:,k) = Gibbs(T,1,phi(k))
end
any ideas?
Stephen23
Stephen23 2018-10-7
编辑:Stephen23 2018-10-7
"It spits out the following error:"
That is a warning, not an error.
"any ideas?"
The algorithm is producing numeric garbage for your input data, and this is unrelated to your original question.
The problem is caused by a combination of your input data (which you have not given us) and the function Gibbs (which you have not given us, or shown us where you got it from) that results in some calculation (inside Gibbs) being performed that returns numeric garbage. But without the complete error message (i.e. all of the red text) and the input data and the function then debugging your code means debugging by guessing, which is not a very efficient way to debug code.
Solution: use different data, or try a different algorithm, or learn that not all problems are numerically solvable using naive implementations of some algorithm.
Search this forum for "Matrix is close to singular or badly scaled" and you will get many discussions on this topic, with very good and detailed explanations. Go and read them.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by