Define a matrix elementwise

I would like to define the matrix Q based on the given matrix A and B (both very large). So can I define like this?
Q(i,j) = A(i,j);
if B(i,j)=0
0; elseif A(i,j)=0
else 1/abs(A(i,j));
end

12 comentarios

James Tursa
James Tursa el 30 de Mzo. de 2016
Your code does not make sense. Can you fix it so that we can understand the algorithm you are trying to implement?
MatLab
MatLab el 31 de Mzo. de 2016
I want to define the following matrix Q, such that Q_ij=A_ij when B_ij=0 Q_ij=0 if A_ij=0, otherwise set Q_ij=1/|A_ij| where A and B are defined in the algorithm
dpb
dpb el 31 de Mzo. de 2016
Which is what the Answer provided does...
>> A=randn(4) % sample data
A =
0.9642 -0.7982 1.3514 -0.8479
0.5201 1.0187 -0.2248 -1.1201
-0.0200 -0.1332 -0.5890 2.5260
-0.0348 -0.7145 -0.2938 1.6555
>> B=ones(4);B(randperm(numel(A),round(0.2*numel(A))))=0 % hit-n-miss zeros in B
B =
1 0 1 1
1 1 1 0
1 0 1 1
1 1 1 1
>> Q=1./abs(A).*(B~=0); Q(isnan(Q))==0;
Q =
1.0371 0 0.7400 1.1793
1.9229 0.9817 4.4490 0
49.9305 0 1.6977 0.3959
28.7595 1.3995 3.4042 0.6040
>>
MatLab
MatLab el 31 de Mzo. de 2016
Thanks, appreciated
MatLab
MatLab el 31 de Mzo. de 2016
So what to do if the Q is not square matrix, can you define a rectangular matrix with all 1?
dpb
dpb el 31 de Mzo. de 2016
Makes no difference as long as A,B same size...and Q is size(A). Did you even try it?
MatLab
MatLab el 2 de Abr. de 2016
Sorry I m new to Matlab, I tried but I m wondering how to define Q as a variable of A,B first. It always say, functions definition are permitted this way
dpb
dpb el 3 de Abr. de 2016
Sorry, don't understand the question...you have an A and B? If so, just type in what I did...Matlab allocates automatically on assignment, there's nothing needed a priori.
MatLab
MatLab el 3 de Abr. de 2016
I understand your code, however, I want to define the function Q(A,B) and employ it in other parts of my algorithm. So I want to define an actual function instead of running it under command menu. Hopefully, this clarifies
Walter Roberson
Walter Roberson el 3 de Abr. de 2016
Editada: Walter Roberson el 3 de Abr. de 2016
function result = Q(A,B)
result = 1./abs(A).*(B~=0);
result(isnan(result))==0;
and store it in Q.m
MatLab
MatLab el 3 de Abr. de 2016
Editada: MatLab el 3 de Abr. de 2016
Thanks so much, this really helps. Appreciated! I wrote function Q(A,B)=result instead, it didn't run
dpb
dpb el 3 de Abr. de 2016
Ah, sorry I didn't follow where the hangup was...note now that if you follow Walter's lead and name the function Q if you subsequently write
Q=Q(A,B);
that by Matlab parsing rules you will have aliased the definition of the function Q by a resulting array Q and won't be able to use the function again until clear Q or change context such that the array isn't in scope or the like.

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 30 de Mzo. de 2016
Editada: dpb el 1 de Abr. de 2016

0 votos

Q=1./abs(A).*(B~=0); % 1/abs(A) and zero B locations
Q(isnan(Q))==0; % fixup 0 locations from A (logical*Inf-->NaN)
ADDENDUM
Or, if one doesn't like the cleanup after the fact, one can do it first...
A(A==0)=inf; % so 1/Inf-->0
Q=1./abs(A).*(B~=0); % 1/abs(A) and zero B locations

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Mzo. de 2016

Comentada:

dpb
el 3 de Abr. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by