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
댓글 수: 0
답변 (2개)
darova
2020년 2월 12일
Use repmat
a = rand(531, 758);
r = rand(531, 1);
R = repmat(r,[1 758])
res = a./R;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!