MATLAB Answers

For loop for matrix division

2 ビュー (過去 30 日間)
quinn rizzo
quinn rizzo 2019 年 11 月 7 日
Edited: John D'Errico 2019 年 11 月 8 日
How would you use a for loop to divide a matrix by a vector if the vector has changing values?

  1 件のコメント

Walter Roberson
Walter Roberson 2019 年 11 月 7 日
If the matrix is A, and the vector is V, then how should the output be constructed? Is Output(J,K) = A(J,K)./V(J) for example ? Is every column to be divided by a different version of the vector, such as (for example) A(:,1)./V, A(:,2)./V.^2, A(:,3)./V.^3 ?

サインイン to comment.

件の回答 (1)

John D'Errico
John D'Errico 2019 年 11 月 7 日
編集済み: John D'Errico 2019 年 11 月 8 日
Why would you want to use any loop at all? For example, if A is an mxn matrix, and V is a vector of size mx1, then consider what does this do:
B = A./V;
At least, if you are using release R2016b or later.
Earlier releases would use bxfun...
B = bsxfun(@rdivide,A,V);
Still pretty easy. If V is a row vector, then similar things apply.
So unless this is homework, why would you bother using a loop at all? And if it is homework, then why not try something? (And tell us honestly that it is your homework.) Show what you would think of doing, and you might get some advice on how to fix it. And you never know, you might even get it right yourself.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by