Effacer les filtres
Effacer les filtres

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 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Image Analyst
Image Analyst le 19 Sep 2015
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

Plus de réponses (1)

Stephen23
Stephen23 le 17 Sep 2015
Modifié(e) : Stephen23 le 17 Sep 2015
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 commentaires
John D'Errico
John D'Errico le 17 Sep 2015
So? Then use a loop, and stuff the results into another matrix.
seema niran
seema niran le 19 Sep 2015
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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by