For loop with matrices
Show older comments
deltao, fo and deltafo are matrices, and i want to calculate part1Fio with changes of f(f=1:0.5:100). I try on this way, but doesnt work. Where I'm wrong?
part1Fio(length(f))=0;
for i = 1:length(f)
part1Fio(i)=(deltafo-deltao.*(fo-f(i)))./((fo-f(i)).^2+deltafo.^2);
end
It display this error "In an assignment A(I) = B, the number of elements in B and I must be the same."
Answers (1)
Andrei Bobrov
on 6 Nov 2013
Edited: Andrei Bobrov
on 6 Nov 2013
part1Fio = zeros([size(fo),numel(f)]);
for ii = 1:length(f)
part1Fio(:,:,ii)=(deltafo-deltao.*(fo-f(ii)))./((fo-f(ii)).^2+deltafo.^2);
end
or
s = size(fo);
[ii,jj,kk] = ndgrid(1:s(1),1:s(2),f);
ij = sub2ind(s,ii,jj);
p1 = fo(ij)-kk;
part1Fio = (deltafo(ij)-deltao(ij).*p1)./(p1.^2 + deltafo(ij).^2);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!