MATLAB Answers

0

hi every one, i need to ask a question, thank you

Asked by mina massoud on 30 Jun 2019
Latest activity Commented on by mina massoud on 1 Jul 2019
if i have a row vector like this A=[ 0 1 1 1 0 0 1 1 0 1 ]
and another vector B=[ 1 2 3 4 5 6 ]
and i need to put the element of B in order in A when the element is equal to 1
so if it can possibile i need to return a vector with the dimensions of B like this [ 0 1 2 3 0 0 4 5 0 6 ] , how can i do it if it's possibile to do it in matlab
thank you

  0 Comments

Sign in to comment.

Products


Release

R2019a

4 Answers

Answer by Image Analyst
on 30 Jun 2019
 Accepted Answer

Try this:
A=[ 0 1 1 1 0 0 1 1 0 1 ]
B=[ 1 2 3 4 5 6 ]
out = A % Initialize
out(logical(A)) = B % Assign B to "1" locations in A
It give you what you asked for.

  0 Comments

Sign in to comment.


Answer by Matt J
on 30 Jun 2019
Edited by Matt J
on 30 Jun 2019

result=sparse(1,find(A),B)

  0 Comments

Sign in to comment.


Answer by Catalytic
on 30 Jun 2019

A=double(A);
A(logical(A))=B;

  0 Comments

Sign in to comment.


Answer by Matt J
on 30 Jun 2019
Edited by Matt J
on 1 Jul 2019

[i,j]=find(A(:));
result=accumarray([i,j],B(:),size(A.')).'

  4 Comments

Show 1 older comment
I've debugged the accumarray solution, though I'm not sure why it was preferred over this one. That was the first answer offered and works straight out of the box.
I think it's because the poster did not see what she wanted to see. So instead of seeing:
0 1 2 3 0 0 4 5 0 6
like she asked for, you see
result =
(1,2) 1
(1,3) 2
(1,4) 3
(1,7) 4
(1,8) 5
(1,10) 6
sorry i accept it by mistake , now i accept your answer that is the best answer
thank's very much for all of you, i really appraciate it

Sign in to comment.