필터 지우기
필터 지우기

How do I input a matrix (variable) in a function?

조회 수: 1 (최근 30일)
hithere
hithere 2014년 10월 22일
댓글: hithere 2014년 10월 22일
I created a function:
.
x is the excitation wavelength where i will input eg. " test,wavelength(765,y)"
.
y is a 729 by 1 matrix: .
.
the result is:
.
.
where only the first number of y matrix is being implemented in the function. How do i create a function where the first number of y matrix will be implemented and return as a result as the first number in wavelengtha matrix and so on.
  댓글 수: 1
the cyclist
the cyclist 2014년 10월 22일
The screenshots are very helpful in understanding your problem. I suggest not using them for the actual code, though, so that we can copy & paste your code into MATLAB ourselves.

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

채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 10월 22일
1) why you have comma in your function name?
2) Change your equation in element wise division as follow:
wavelengtha= 1./ ((1./x)-(y/10^7))
  댓글 수: 3
Mohammad Abouali
Mohammad Abouali 2014년 10월 22일
One way is to do this:
let's say x=[765, 766, 777]
then you can do this:
for i=1:numel(x) wavelengtha{i}= yourFunction(x(i), ...) end
I still don't get the comma in your function name?
hithere
hithere 2014년 10월 22일
sorry i have rename my function to just
.
function [wavelengtha] = test(x,y)
wavelengtha = 1./ ((1./x)-(y/10^7));
for i=1:numel(x)
wavelengtha{i}= test(x(i), ...);
end
end
. However i cant get the for loop to work.

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

추가 답변 (1개)

the cyclist
the cyclist 2014년 10월 22일
I'm not sure I fully understand what you mean, but does this do what you intend?
wavelengtha = 1./((1./x) - (y/10^7));
Notice that I used element-wise division.
  댓글 수: 1
hithere
hithere 2014년 10월 22일
Thank you for you help!
As far as i understand. the result will return as a new variable in my workspace. Even when i change the input of x. The resulted variable will replace my previous work. is there a way to have the resulting variable number somewhat related to my input x eg(x = 765. then the wavelengtha out automatically output as wavelengtha765 something like that)

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

카테고리

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