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

4 次查看(过去 30 天)
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)

采纳的回答

Star Strider
Star Strider 2015-9-25
编辑:Star Strider 2015-9-25
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 个评论
Image Analyst
Image Analyst 2015-9-25
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.
Star Strider
Star Strider 2015-9-25
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.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-9-25
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:

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by