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

조회 수: 1 (최근 30일)
mina massoud
mina massoud 2019년 6월 30일
댓글: mina massoud 2019년 7월 1일
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

채택된 답변

Image Analyst
Image Analyst 2019년 6월 30일
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.

추가 답변 (3개)

Matt J
Matt J 2019년 6월 30일
편집: Matt J 2019년 6월 30일
result=sparse(1,find(A),B)

Catalytic
Catalytic 2019년 6월 30일
A=double(A);
A(logical(A))=B;

Matt J
Matt J 2019년 6월 30일
편집: Matt J 2019년 7월 1일
[i,j]=find(A(:));
result=accumarray([i,j],B(:),size(A.')).'
  댓글 수: 4
Image Analyst
Image Analyst 2019년 7월 1일
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
mina massoud
mina massoud 2019년 7월 1일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by