saving from for loop

조회 수: 1 (최근 30일)
Jakub
Jakub 2011년 12월 21일
Hi guys
I have an array a and b. I want to divide all of the elements from row 1 of array a by the first element from array b and all of the elements from row 2 of array a by second element from array b etc.
Here is an example and what I wrote: a = [1,2,3,4;5,6,7,8;9,10,11,12]; b = [50,60,70];
c = zeros(size(a));
for i=1:length(b)
c(i) = a(i,:)./b(1,i)
end
It does not work and the eror is: In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in try (line 7) c(i) = a(i,:)./b(1,i).
Second thing, I wanted to pre locate the space for results so I used zeros function; is that the best way to allocate space for result from for loop and did I use it correctly in that case?
Thanks in advance jakub

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 12월 21일
Very close. c(i,:) = a(i,:)./b(1,i)
Yes. You did it right pre-allocating.
  댓글 수: 1
Jakub
Jakub 2011년 12월 21일
Hi,
Thanks so much!!!
stupid me.
cheers
jakub

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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