필터 지우기
필터 지우기

How can I match these vector values?

조회 수: 1 (최근 30일)
Robert
Robert 2014년 12월 11일
답변: Image Analyst 2014년 12월 11일
I have 2 vectors. One is 1505 values long with 200 1s spread throughout and the rest NaNs. The other is only 200 values long. I want to space out the 200 value long vector so that those 200 values match the position of the 1s in the 1505 long vector as they represent where the data should be positioned.
I am a matlab newbie so as much detail as possible would be appreciated.
Thanks in advance for any help!
  댓글 수: 1
John D'Errico
John D'Errico 2014년 12월 11일
It is time for you to learn the basics of MATLAB. This is basic indexing. Start reading the tutorials.
help find

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

답변 (2개)

Star Strider
Star Strider 2014년 12월 11일
Our friend accumarray may work best here. First, you would need to use the find command to get the index positions of the ‘1’ values in the longer vector to get my ‘ix1’ vector. My ‘rv’ vector corresponds to your 200-length vector. ‘A’ is the output column vector with zeros everywhere other than the positions where accumarray put the elements of ‘rv’. Note that ‘A’ is not the same length as your original NaN vector, so you will have to zero-pad it at the end if you need them to be equal. Transpose ‘A’ to be a row vector if necessary.
The code:
ix1 = randi(100,1,10); % Index Positions Of ‘1’ Values
rv = randi(20,1,10); % Values In ‘200’ Vector
A = accumarray(ix1', rv); % Assign Values To New Vector At ‘1’ Index Positions

Image Analyst
Image Analyst 2014년 12월 11일
Here's another way:
% Set up/initialize data
indexesOf1s = sort(randperm(1500, 200))
m1505 = zeros(1,1505);
m1505(indexesOf1s) = 1;
m200 = randi(9, 1, 200);
% The above was all setup to get your two arrays.
% Now, do it. Put the values from the m200 array
% into the locations of the 1's of the m1505 array.
output = m1505; % Initialize length
logicalIndexesOf1s = m1505 == 1; % Find where m1505 is 1. Logical index.
output(logicalIndexesOf1s) = m200; % Stuff actual values in there.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by