For loop with matrices

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."

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2013-11-6
编辑:Andrei Bobrov 2013-11-6
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);

类别

帮助中心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!

Translated by