필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Ali Kareem
Ali Kareem 2015년 9월 25일
댓글: Star Strider 2015년 9월 25일
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