How to feed a function's output later on?

I need to assign output to a function through a program not when I define the function. For example f(.2,-.5)=4 and in a few line later f(-3,4.5)=-5. This is going to save other function's output. It's important that indices to be non-integer and negative so I cannot use a matrix for saving these date. How can I do this? Thanks in advance.

4 件のコメント

dpb
dpb 2014 年 8 月 17 日
編集済み: dpb 2014 年 8 月 17 日
How do you "know" f(x,y) is 4 or -5 given the particular inputs? That's how the function value is given (if it is a function).
Alternatively, looks like you could simply use a 3*N array of z=f(x,y) to store the x,y and f(x,y) values.
Adam
Adam 2014 年 8 月 17 日
A matrix is capable of storing non-integer negative data so I'm not sure what you mean that you cannot use a matrix.
John D'Errico
John D'Errico 2014 年 8 月 17 日
Store the values of [x,y,f(x,y)] in one array, as rows (or columns) of an Nx3 or 3xN array.
What is the problem? If you have something special that you need, you need to be more explicit.
stavanger
stavanger 2014 年 8 月 18 日
編集済み: stavanger 2014 年 8 月 18 日
Thanks for your comments. Please look down on the reply to the answer for clarification of my question

サインインしてコメントする。

 採用された回答

Iain
Iain 2014 年 8 月 18 日

0 投票

I reckon this will do it:
function z = f(x,y)
persistent old_x old_y old_z
if isempty(old_x)
old_x = x;
old_y = y;
z = calculate the answer
old_z = z;
else
z = old_z(x == old_x & y == old_y);
if isempty(z) % old result doesn't exist, need to recalculate
old_x(end+1) = x;
old_y(end+1) = y;
z = calculate the answer
old_z(end+1) = z;
end

5 件のコメント

stavanger
stavanger 2014 年 8 月 18 日
編集済み: stavanger 2014 年 8 月 18 日
It seems interesting. I need a little time to figure out how does this work for me. Also, It seems that it needs extra 'end' at the end. Thanks for your answer.
stavanger
stavanger 2014 年 8 月 19 日
Could you please clarify your code. I don't understand some part of it.
z = old_z(x == old_x & y == old_y);
and the indices of
end+1
stavanger
stavanger 2014 年 8 月 19 日
I checked your answer by inserting extra command into your code to see whether it actually inhibit recalculating for the same input and I found out that it actually does.
If I type ff(3,2) in command line for the first time it gives me 3 and 6 (2*3). retyping f(3,2) it gives just 6, even after recalling function for some other input, it remember that f(3,2) has been calculated and gives only 6. It gives me extra 3 only for new inputs. EXCELLENT! THANK YOU SO MUCH.
But I can't figure out how this code do this job :(
function z = ff(x,y)
persistent old_x old_y old_z
if isempty(old_x)
old_x = x;
old_y = y;
z = x.*y;
2
old_z = z;
else
z = old_z(x == old_x & y == old_y);
if isempty(z) % old result doesn't exist, need to recalculate
old_x(end+1) = x;
old_y(end+1) = y;
z = x.*y;
old_z(end+1) = z;
3
end
end
Iain
Iain 2014 年 8 月 19 日
persistent old_x old_y old_z
What that does is, it says to the function "remember these values the next time you get run, to save time".
The first if statement determines if the code has been run before. If not, it just calculates the result, and updates old_x, old_y and old_z with the new result.
If it has run before, it attempts to read an old answer
z = old_z(x == old_x & y == old_y);
If it couldn't read an answer, it calculates the answer and updates old_x, old_y and old_z with the new result.
old_x(end+1) = x;
That means append the value of x to the "old_x" vector.
stavanger
stavanger 2014 年 8 月 19 日
編集済み: stavanger 2014 年 8 月 19 日
I got it now. Fantastic!
Cheers,

サインインしてコメントする。

その他の回答 (1 件)

Adam
Adam 2014 年 8 月 18 日

0 投票

By the sounds of it some kind of encoding of your x and y values to a single key is what you need. This key can then be used with the corresponding f(x,y) result in a
containers.Map
object.
If you are familiar with maps in C++ or some other language Matlab maps can seem a bit strange, but they are good for this type of thing if your x and y values are not of a contiguous variety that could easily form the rows and columns of a 2d matrix.
What is the best way to encode an (x,y) pair into a single key to provide a map lookup for that value though depends on the range of your x and y values. I'm certainly not an expert on encoding data into keys, but for just 2 values it should be quite simple to find an encoding that ensures uniqueness for every valid (x,y) pair.

1 件のコメント

stavanger
stavanger 2014 年 8 月 19 日
Thanks for your answer. lain's answer solved my problem

サインインしてコメントする。

カテゴリ

製品

質問済み:

2014 年 8 月 17 日

コメント済み:

2014 年 8 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by