Inserting data of one matrix into another

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

 Risposta accettata

Adam
Adam il 26 Set 2014
Modificato: Adam il 26 Set 2014
a(a ~= 0) = b

10 Commenti

That's right where my thinking is, but it's not changing anything about a after that line of code! If I display a right after the line you provided, it is still 0's and 1's.
It works fine when I try it! And also for your other example. Are you sure you typed it exactly the same way?
No, it shouldn't, given the provided data. Please post the code you are using.
Interesting. It works for me, replacing the appropriate elements of ‘a’ with the elements of ‘b’.
Chris
Chris il 26 Set 2014
Modificato: Chris il 26 Set 2014
Well it doesn't matter to me if it creates a new vector or overwrites the original data of vector "a" - let's just change "a" for now since that's what I stated originally.
Here's my actual code
d = S \ P % Vertical vector of -0.89955, -0.001057
U = all(K)'; % Vertical vector of 0 0 1 1 0 0
U(U ~= 0) = d % Displays vertical vector of 0 0 1 1 0 0
Adam
Adam il 26 Set 2014
Modificato: Adam il 26 Set 2014
Ah well, your vector is logicals, not doubles! That makes it more, well...logical...in terms of syntax!
Since you don't care if it is in the same vector or a different vector then
c(a) = b;
c = c';
should work, inserting your own variable names as appropriate. There's probably a slightly neater way of getting back to a vertical vector. You can pre-allocate c as:
c = zeros(size(a));
if you prefer or if you don't care whether your result is a row vector or a column vector you can leave off the c' line. I don't generally like code myself though that switches between column and row vectors mid-way through a calculation.
José-Luis
José-Luis il 26 Set 2014
Modificato: José-Luis il 26 Set 2014
This should work:
d = [-0.8; -0.001];
U = [0 0 1 1 0 0]';
U(U~=0) = d
If it doesn't, then d and U (or both) probably don't contain what you think they do. Also, the common lingo for "vertical vector" is "column vector".
Chris
Chris il 26 Set 2014
Modificato: Chris il 26 Set 2014
Adam, pre-allocating c and then using
c(a) = b;
worked great, thanks so much for the help everyone.
Please accept the answer of it solved your problem.
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!

Accedi per commentare.

Più risposte (2)

Stephen23
Stephen23 il 3 Apr 2015
Modificato: Stephen23 il 3 Apr 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 Commenti

any insight on my issue stephen? I would really appreciate any help.
Stephen23
Stephen23 il 3 Apr 2015
Modificato: Stephen23 il 3 Apr 2015
"I have a slightly different issue..." → ask a new question.
I posted the question :Inserting data into matrix of zeros from another matrix.

Accedi per commentare.

LUI PAUL
LUI PAUL il 3 Apr 2015
Modificato: LUI PAUL il 3 Apr 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 Commenti

You have totally missed the issue. The 1's & 0's vector "a" is logical. So your scheme doesn't work. E.g.,
>> a = logical([0 0 1 1 0 0]);
>> b = [5 6];
>> p = find(a>0);
>> a(p) = b
a =
0 0 1 1 0 0
its not mentioned 'logical'.Chris said only vector....logical may not be used....
Go to Adam's answer. Read the 5th and 6th comments by Chris and Adam. They clearly show that the fundamental issue is that "a" is logical, and Adam posts a solution for this that works when "a" is logical.
LUI PAUL
LUI PAUL il 3 Apr 2015
Modificato: LUI PAUL il 3 Apr 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?

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

il 26 Set 2014

Commentato:

il 3 Apr 2015

Community Treasure Hunt

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

Start Hunting!

Translated by