Can't get function to accept array params or return arrays

Consider this function to calculate the slope of a line:
function [m] = f_GetSlope(x1, y1, x2, y2)
m = (y1 - y2)/(x1 - x2);
It works with scalar inputs. But I want to call it like so:
aX1 = [1, 23];
aY1 = [1, 12];
aX2 = [2, 8];
aY2 = [2, 0];
m1 = f_GetSlope(aX1, aY1, aX2, aY2)
Ideally it would return:
m1 = 1 0.800
In actuality:
m1 = 0.8009
Just one return value, and not correct for either set of inputs.
What am I doing wrong?

 채택된 답변

Cedric
Cedric 2013년 4월 13일
편집: Cedric 2013년 4월 13일
You want to use an element-by-element operator for the division:
m = (y1 - y2)./(x1 - x2);

댓글 수: 2

Interesting.
Cedric
Cedric 2013년 4월 13일
편집: Cedric 2013년 4월 13일
Yes, some operators have a mathematical definition when applied on matrices that is different from the element-wise type of operations.
The most typical case is probably the matrix multiplication * (performed according the the mathematical def. of matrix multiplication), which is completely different from the element-wise multiplication .* (multiplication element-by-corresponding-element between two arrays with same size).

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2013년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by