Use elements from different Matrices as function input

5 visualizaciones (últimos 30 días)
Fred John
Fred John el 31 de Dic. de 2014
Comentada: Fred John el 31 de Dic. de 2014
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 comentario
dpb
dpb el 31 de Dic. de 2014
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?

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 31 de Dic. de 2014
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

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by