How can I store the output of a function in a seperate matrix?

1 次查看(过去 30 天)
The function has an input of 130 coordinates (in a 130x2 matrix). The output is a 5x1 vector for each pair of coordinates. Right now when I run the function in the command window, the output is displayed in the command window (so lots of 5x1 vectors). I would like these to be stored in a matrix so I can export all of them at once in a .csv file. How can I do this? The code I have displayed below is for the function.
function [out_array] = compute_c(in_array)
for i = 1:size(in_array, 1)
x_amber = 0.657815;
y_amber = 0.3334;
x_green = 0.2956;
y_green = 0.6143;
x_blue = 0.1602;
y_blue = 0.0187;
x_white_2500K = 0.477;
y_white_2500K = 0.4136;
x_white_6500K = 0.3135;
y_white_6500K = 0.3236;
Y_mix = 40;
A = [((x_amber - in_array(i, 1))/y_amber) ((x_green - in_array(i, 1))/y_green) ((x_blue - in_array(i, 1))/y_blue) ((x_white_2500K - in_array(i, 1))/y_white_2500K) ((x_white_6500K - in_array(i, 1))/y_white_6500K)
((y_amber - in_array(i, 2))/y_amber) ((y_green - in_array(i, 2))/y_green) ((y_blue - in_array(i, 2))/y_blue) ((y_white_2500K - in_array(i, 2))/y_white_2500K) ((y_white_6500K - in_array(i, 2))/y_white_6500K) 1 1 1 1 1];
B = pinv(A);
out_array(:, i) = B*[0; 0; Y_mix];
end
end

采纳的回答

Stephen23
Stephen23 2016-8-12
编辑:Stephen23 2016-8-12
You do not need to call this function in a loop
Get rid of the loop when you call the function.
You don't state this in your question, but presumably you are calling this function in a loop. The answer to your question would be then to stop calling the function in a loop. During my testing of your function it works white well with a 130x2 input matrix, and returns the result for each pair (row) as a column in the output matrix. Why does it work? Because the function already has a loop built into it, and it already puts all of those vectors into one matrix.
So you are asking how to do something that the function already does!
All you need is this:
[5x130 matrix] = compute_c([130x2 matrix])
for example:
outMat = compute_c(inpMat)
Addendum This question is a classic example of why badly written code causes more problems than it solves. The author of this code did not write any code comments, did not explain any of the algorithm, added no help section, no references, the code is badly formatted and had random empty lines that make it hard to follow.
If this code had been correctly formatted, indented, and had some comments explaining what it does, then this question would have been answered by simply reading the code itself.
Although beginners seem to think that this advice is just given by grumpy academics trying to get in the way of them "writing real code", in fact code best practice of writing comments, indention, formatting correctly, etc, helps everyone to understand what code does, how to use it, and how to fix it...

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-12
for k=1:10
in_array=? % Define your in_array
out_array = compute_c(in_array)
M{k,1}=out_array
end
M=cell2mat(M)
xlswrite('file.csv',M)
  1 个评论
Stephen23
Stephen23 2016-8-12
编辑:Stephen23 2016-8-12
There is absolutely no point to this loop. The function already includes a loop, which does exactly the same thing as this answer.
See my answer for the simplest solution.

请先登录,再进行评论。

类别

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