creating a function for my repeating computation

조회 수: 1 (최근 30일)
fafz1203
fafz1203 2016년 10월 18일
답변: Walter Roberson 2016년 10월 18일
I want to create a function for the following set of repeated computations i will be performing in my work.
d = ((x*b) - y).^2;
test = sum (d(:))/1000;
Your help is much appreciated.
  댓글 수: 1
fafz1203
fafz1203 2016년 10월 18일
please know that the value of x and y is different on every call, it takes different values of x and y every time it's called.

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

답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 18일
test = @(b, x, y) sum( reshape( (x*b - y).^2, [], 1) ) / 1000;
Now
result = test(b, x, y); %a call to the function
Question: do there happen to be exactly 1000 elements in y, so it is computing a mean? If so then
test = @(b, x, y) mean2( (x*b - y).^2 ); %if you have the image processing toolbox
test = @(b, x, y) mean( reshape( (x*b - y).^2, [], 1) ); %if you do not have image processing
For some shapes of x and b and y, it would be possible to use (:) instead of reshape()

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by