Effacer les filtres
Effacer les filtres

How to avoid a loop using vectors?

1 vue (au cours des 30 derniers jours)
Yuval
Yuval le 9 Nov 2013
Hi, Is there a way to avoid using a for loop (or any loop for that matter) in the following?:
h=(10^-1).^[1:8];
x=pi;
r=5e-6.*rand(16,1);
for i=1:8
fminush(i)=cos(x-h(i))+r(i);
fplush(i)=cos(x+h(i))+r(i+8);
end
fder=(fplush-fminush)./(2*h);
plot(log(h),log(abs(-sin(x)-fder)))
I have tried redefining r to be of size 8 then adding it to both fminush and fplush, to no avail. Matlab would still alert for "matrix dimensions must agree". I'd be thankful for some advice.

Réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 9 Nov 2013
Modifié(e) : Azzi Abdelmalek le 9 Nov 2013
h=(10^-1).^[1:8];
x=pi;
r=5e-6.*rand(1,16);
fminush1=cos(x-h)+r(1:8);
fplush1=cos(x+h)+r(9:16)
fder=(fplush-fminush)./(2*h);
plot(log(h),log(abs(-sin(x)-fder)),'r')

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by