Use elements from different Matrices as function input

조회 수: 5 (최근 30일)
Fred John
Fred John 2014년 12월 31일
댓글: Fred John 2014년 12월 31일
Hi
I previously asked a question which was resolved here:
My follow up question is: how can I use an element from a different Matrix (B2(1,1) & B2(1,2)) at the same time?
My guess was
i
function [S] = myFunction(A,B)
q=A(1,1);
w=A(1,2);
h=B2(1,1);
k=B2(1,2);
Can someone help or verify? Thanks!
  댓글 수: 1
dpb
dpb 2014년 12월 31일
Don't follow the question, precisely? Of course you can associate any element of any array with a temporary variable as you've outlined, but why not just use the reference directly?
Answering my own (admittedly somewhat rhetorical) question, there is one reason in that it's less typing to use the single-character variable names or there may be a physical interpretation of the elements such that it's convenient for documentation purposes of reading the code. But, it's immaterial as far as Matlab is concerned.
So, again, what's the real issue behind the question?

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

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 12월 31일
Fred - you have the right idea. If you want to use data from both A and B, then pass them both as input parameters to your function and access the data like you have done (except use B instead of B2)
function [S] = myFunction(A,B)
q=A(1,1);
w=A(1,2);
h=B(1,1);
k=B(1,2);
S = [];
% set S with q, w, h, and k

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by