Fill the array after each loop iteration

조회 수: 317(최근 30일)
Ali Jaber
Ali Jaber 2016년 4월 24일
편집: Ali Jaber 2016년 4월 24일
my Problem is that I want the array to be filled each time the loop iterate, for example here, cosTheta is my array, I want after each iteration to add the new value to it as an element. This is what I wrote:
for k=1:10
r(k) = (dot(a,b)/(norm(a)*norm(b)));
cosTheta = [r(1) r(2) r(3) r(4) r(5) r(6) r(7) r(8) r(9) r(10)];
But I think this is not a good idea if I want a larger number of iteration.. for example if k will go from 1 to 50, this not an ideal way to do that, can you help me plz

채택된 답변

Stefan Raab
Stefan Raab 2016년 4월 24일
why use the r at all? Try
n_iter = 10;
cosTheta = zeros(1,n_iter); % Memory preallocation
for k=1:n_iter
a = trainZ(k,:);
b = testZ;
cosTheta(k) = (dot(a,b)/(norm(a)*norm(b)));
Kind regards, Stefan
  댓글 수: 1
Ali Jaber
Ali Jaber 2016년 4월 24일
Thank you :) This was my problem

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

추가 답변(0개)


Community Treasure Hunt

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

Start Hunting!

Translated by