Borrar filtros
Borrar filtros

Inserting data of one matrix into another

4 visualizaciones (últimos 30 días)
Chris
Chris el 26 de Sept. de 2014
Comentada: James Tursa el 3 de Abr. de 2015
If I have a vector a
0
0
1
1
0
0
and a vector b
5
6
and I want to input the data of b into the nonzero elements of a (which will always be together and matching the dimensions of b), so that vector c reads
0
0
5
6
0
0
What is an easy way to do this? Thank you!
Another example that it needs to work for:
a b c
_ _ _
0 4 0
0 8 0
1 3 ----> 4
1 7 8
1 3
1 7

Respuesta aceptada

Adam
Adam el 26 de Sept. de 2014
Editada: Adam el 26 de Sept. de 2014
a(a ~= 0) = b
  10 comentarios
José-Luis
José-Luis el 26 de Sept. de 2014
Please accept the answer of it solved your problem.
Shane Hagen
Shane Hagen el 3 de Abr. de 2015
I have a slightly different issue maybe someone can help?
I have a matrix [signal] of 315954x64 of signal data. In another matrix [FFlash] (155520x1) there is logical 1 or 0 depending on an activation
I have categorized the signal matrix to obtain a matrix [FFsignal] (155520x64) of data when there is an activation
To graph I need matrices of similar dimensions so I wanted to insert the categorized data into a matrix of zeros of size (315954x64)
For example the first group of activation is in rows 631-654 and when categorized I have data for those time points. I want to add this data to a matrix of zeros in the same time points if possible. Therego, zeros until 631-654 and so on through the set. Please help!

Iniciar sesión para comentar.

Más respuestas (2)

Stephen23
Stephen23 el 3 de Abr. de 2015
Editada: Stephen23 el 3 de Abr. de 2015
MATLAB's powerful indexing makes this easy, if we use logical indexing:
>> a = [false;false;true;true;false;false];
>> b = [5;6];
>> c = zeros(size(a));
>> c(a) = b
c =
0
0
5
6
0
0
And the same for the second example:
>> a = [false;false;true;true;true;true];
>> b = [4;8;3;7];
>> c = zeros(size(a));
>> c(a) = b
c =
0
0
4
8
3
7
  3 comentarios
Stephen23
Stephen23 el 3 de Abr. de 2015
Editada: Stephen23 el 3 de Abr. de 2015
"I have a slightly different issue..." → ask a new question.
Shane Hagen
Shane Hagen el 3 de Abr. de 2015
I posted the question :Inserting data into matrix of zeros from another matrix.

Iniciar sesión para comentar.


LUI PAUL
LUI PAUL el 3 de Abr. de 2015
Editada: LUI PAUL el 3 de Abr. de 2015

try simple

a=[0;0;1;1;0;0];

b=[5;6];

p=find(a>0);

a(p)=b

a =

     0
     0
     5
     6
     0
     0
  5 comentarios
LUI PAUL
LUI PAUL el 3 de Abr. de 2015
Editada: LUI PAUL el 3 de Abr. de 2015
for logical a,...try this
a = logical([0 0 1 1 0 0]);
a=double(a);
b = [5 6];
p = find(a>0);
a(p) = b
a =
0 0 5 6 0 0
what do you think @James will it work?
James Tursa
James Tursa el 3 de Abr. de 2015
Yes.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by