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

Respuestas (1)

Andrei Bobrov
Andrei Bobrov el 6 de Nov. de 2013
Editada: Andrei Bobrov el 6 de Nov. de 2013

0 votos

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);

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Nov. de 2013

Editada:

el 6 de Nov. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by