Saving data from equation in Marix or Grid then call them later

In Mathlab if we have
U=f(Y)+f(Z)
Y have 20 different values
Z have 100 different values
I mean if Y=1 then Z will have 100 different values. For each Y there will be 100 values for Z. this mean I will have 100*20 matrix.
My question is in Mathlab how I can save the output from any equation in any matrix or grid? for this case (100*20)
For example U=Y*Z (when Y=1 then Z have 100 values. Then when Y=3.2 then Z will have 100 new different values)
I mean I need to save the result from any equation in matrix or grid using Mathlab. I need to call these numbers later to use in another function. In addition how I can call each number in this matrix to use it in another equation and how I can drew this numbers ( I mean the number in matrix or grid)

 Accepted Answer

Use the bsxfun function:
f = @(x) sin(x + cos(x));
Y = 0:19;
Z = 1:100;
U = bsxfun(@plus, f(Y), f(Z).'); % Transpose (.') So One Is A Column Vector
Pass ‘U’ to your other function as an argument to it.

6 Comments

Hi,
Thank you for your reply. Please if I did that when I need to recall any values in matrix in order to use it in another function how I can do that?
If you are calling the function from the script that calculated ‘U’, pass it to your function as an input argument.
If you need to use ‘U’ in another script, do as Image Analyst mentioned, and save ‘U’ to a .mat file and then load it in your other script. See the documentation on save and load for those details.
Hi, Thank you so much. Can I accomplish that by using
For i=1:20
For j=1 :100
M(i,j)=f(y)+f(z)
End
to save this number in matrix? where i, j is the row and column of matrix
That will not work as you have written it. Use my code (using bsxfun) instead.
If the 20 values y takes on are 1-20 and the 100 values z takes on are 1-100, and f() is some function you wrote to operate on some single input value, then I think this should work for calculating a 2D matrix "M":
for y=1:20
for z=1 :100
M(y, z) = f(y) + f(z);
end
end
If y and z are some predefined list of weird numbers and the 1-20 and 1-100 are just indexes into those arrays, then you'd do it this way:
for col=1:20
for row=1 :100
M(row, col) = f(y(col)) + f(z(row));
end
end
Not as compact as the way Star showed you though.
OP’s loops will only work if the arguments to the function are also subscripted, the reason I wrote that it would not work as written:
for i=1:20
for j=1 :100
M(i,j)=f(y(i))+f(z(j));
end
end
When timed, bsxfun is much faster than repmat and signficantly faster than a loop.
And then there’s all the bother about using ‘i’ and ‘j’ as variables.

Sign in to comment.

More Answers (1)

You can save them to a .mat file with save() and recall them with load(), or you can pass them via an argument list, or you can use setappdata/getappdata, or make them global, or attach them to handles structure (if you're using GUIDE). See the FAQ:

Categories

Community Treasure Hunt

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

Start Hunting!