Create a function from a table

조회 수: 2 (최근 30일)
alpedhuez
alpedhuez 2018년 3월 24일
댓글: Star Strider 2018년 3월 24일
Suppose I have a matrix
A = [ 3 0.1; 1 0.2; 2 0.3];
I now want to have a function that takes an entry of the first column and gives the entry in the second column.
In other words, I want to have a matrix B where, for each element in the first column, its second column is the entry of the corresponding second column in A. For example,
B = [ 2 0.3; 2 0.3; 3 0.1; 1 0.2];
Please advise.

채택된 답변

Star Strider
Star Strider 2018년 3월 24일
Try this:
A = [ 1 0.1; 2 0.2; 3 0.3];
A2fcn1 = @(k) A(k,2);
SecondColumn = A2fcn1(2)
SecondColumn =
0.2000
If you want to interpolate instead, use the interp1 (link) function.
  댓글 수: 2
alpedhuez
alpedhuez 2018년 3월 24일
Thank you. Let me work on it.
Star Strider
Star Strider 2018년 3월 24일
As always, my pleasure.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 3월 24일
Given any finite sets of example values, there are aleph-one (the infinity of cardinal numbers) functions that will exactly calculate the given values to within round-off error. There is no way to automatically choose between these functions as one being "better" than the other -- not unless you specify before-hand which forms of function are to be preferred.
  댓글 수: 1
alpedhuez
alpedhuez 2018년 3월 24일
Not that complicated. The question is how to efficiently fill in the second column of B given A and the first column of B.

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by