Can Matlab do this without using a for loop?

Hi, I'm new to Matlab and need some help. I'm using a for loop to calculate all Rs from all combination of A and B. How can I do that?Can Matlab do this without using a for loop?
this is my code
A=(-15:0.175:-1); B=(8:0.1:16);
R = zeros(81);
for i = 1:8
for j =1:81
C = A(i)* erf(x) + B(j);
R = (y-C).^2;
end
end
end
thanks,

 채택된 답변

Sean de Wolski
Sean de Wolski 2013년 1월 14일

0 개 추천

It looks like you are only saving the output from the final iteration.
If you wanted a matrix of every combination, this should do it:
A=(-15:0.175:-1); B=(8:0.1:16);
R2 = (y - bsxfun(@(a,b)a*erf(x)+b,A,B.')).^2

댓글 수: 2

Ali
Ali 2013년 1월 15일
thank you but I tried that and I have a problem with matrix dimensions
Error using + Matrix dimensions must agree.
Error in @(a,b)a*erf(x)+b
both y and x have the size of 1 1000 both A and B have a size of 1 81
Ali
Ali 2013년 1월 15일
I just realized that I need to make A and B the same size as x. Is it possible to expand bsxfun to include 4 parameters A, B ,C and D?
Thanks for your help

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

Ali
2013년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by