Hello,
Consider a 100x1 matrix, called A, that contains random values of "0" and "1".
The number of "1" is 25 and the rest 75 are "0".
Also, consider a matrix 25x1, called B, that contains random values.
How can I replace the "1" of matrix A with the values of matrix B?
The "0" of A would not be influenced.
Thank you.
Best,
Pavlos

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 13일

0 개 추천

A(A==1)=B

댓글 수: 2

pavlos
pavlos 2014년 2월 13일
Thank you for your response.
Unfortunately, the above code do not work.
It works only when A and B have the same dimensions.
In my case, A is 100x1 and B is 25x1.
The number of "1" in A are 25.
This is not true. Look at this example
%exmple
A=zeros(100,1);
A(randperm(100,25))=1
B=rand(25,1)
%----------------------
A(A==1)=B

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

추가 답변 (1개)

Dishant Arora
Dishant Arora 2014년 2월 13일

0 개 추천

A(find(A==1))=B;

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 13일
Why to add find when you can do it without it?
Dishant Arora
Dishant Arora 2014년 2월 13일
yeah,got it

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 2월 13일

댓글:

2014년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by