How to use a for loop to iterate through all columns in a row, and then go down each row and iterate through all the corresponding columns in the same way?

3 ビュー (過去 30 日間)
Hi, so I have a vector called Currents (size is 531 by 758), and a vector called r (size is 531 by 1). My equation is V=Currents/r. Using this equation, I need to create a new vector V, which has the same size as Currents. How can I use a for loop to go through all 758 columns corresponding to each row, and calculate V for each of those columns using the corresponding r value for that row, and then repeat this same step for each row 531 times until the end? Any help would really be appreciated!!!
EDIT: Here is the code I was writing. It isn't giving me the correct output though.
V=zeros(size(Currents))
for i=1:length(r)
for j=1:length(Currents)
V(i,j)=Currents(i,j)/r(i)
end
end

回答 (2 件)

KSSV
KSSV 2020 年 2 月 12 日
V = Currents./r ;
Where Currents is your 531*758 matrix and r is 531*1 matrix.
  5 件のコメント
KSSV
KSSV 2020 年 2 月 12 日
Why did you use *?
Currents = rand(531,758) ;
r = rand(531,1) ;
v = currents./r ;
Zuha Yousuf
Zuha Yousuf 2020 年 2 月 12 日
I didn't use that. What I'm saying is, there is a different r value to be used for the calculation for each row. So we do need to use a for loop. I already have the values for Currents and r, so why are we initializing them using rand?

サインインしてコメントする。


darova
darova 2020 年 2 月 12 日
Use repmat
a = rand(531, 758);
r = rand(531, 1);
R = repmat(r,[1 758])
res = a./R;

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by