i have a matrix A of 1 column and another B of 4 columns. need to do some operations of A with each column of B and to get answer as C1,C2,C3,C4 using loops . Is it possible?

1 次查看(过去 30 天)
i have a matrix A of 1 column and another B of 4 columns. need to do some operations of A with each column of B and to get answer as C1,C2,C3,C4 using loops . Is it possible?

采纳的回答

Image Analyst
Image Analyst 2015-9-19
Perhaps this, which will do a fit of each column of (the badly-named) B vs. A:
% A is the x values,
% Columns of B are the y values
for col = 1 : size(B, 2)
thisColumn = B(:, col);
% Fit a line through these data points in this column.
coefficients = polyfit(A, thisColumn, 1);
slopes(col) = coefficients(1);
intercepts(col) = coefficients(2);
end

更多回答(1 个)

Stephen23
Stephen23 2015-9-17
编辑:Stephen23 2015-9-17
The best solution would be to not create numbered variables, but to simply keep all of the data together in one numeric array. Doing this makes MATLAB code much faster and neater. You can simply use indexing to access that parts of that array than you need to.
If you tell us what those "operations" are then we could tell you effective and efficient ways of doing those operations.
Whatever you do you should not create those variables dynamically. This is a poor programming practice that beginners seem to love. Read this to know why creating variables dynamically is a really bad idea:
  3 个评论
seema niran
seema niran 2015-9-19
i tried m= ones([4749 24]); then to multiply answer during each iteration to the columns of m. but it gives me a [4749 1 ] matrix. how to get a [4749 24] matrix as the final answer?

请先登录,再进行评论。

类别

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