vlookup equivalent function in matlab to add data where an input exist

조회 수: 1 (최근 30일)
Locks
Locks 2014년 9월 16일
댓글: Guillaume 2014년 9월 17일
Hi,
I have the following problem and did not find any solution that is helping me to get the problem fixed.
First there is the vector with all the dates that exist
dates=[1 2 3 4 5 6 7 8 9 10]'
Now I like to create a 10x3 matrix with the dates as the first column:
dataFinal=zeros(length(dates),3);
dataFinal(:,1)=dates;
That's working fine. Now I'd like to fill this matrix dataFinal with values from other matrices, but only there where the first value of the new matrix (data1 or data 2) is equivalent with the first column of dataFinal
data1=[2 22; 3 24; 4 18; 6 22; 7 25; 9 32]
data2=[1 10; 3 14; 5 19; 6 8; 10 15]
The output should look as follows
dataFinal=[1 0 10; 2 22 0; 3 24 14;4 18 0; 5 0 19 and so on]
I tried to use ismember but I get some problems with the dimensions, because data1 and datafinal do not have the same number of rows
regards

채택된 답변

Guillaume
Guillaume 2014년 9월 16일
ismember is indeed the function you need:
[found1, ind1] = ismember(dates, data1(:, 1));
[found2, ind2] = ismember(dates, data2(:, 1));
dataFinal(found1, 2) = data1(ind1(found1), 2);
dataFinal(found2, 3) = data2(ind2(found2), 2);
  댓글 수: 2
Locks
Locks 2014년 9월 16일
looks good.
can you tell me in addition, how I can plot this without the remaining zeros? in the original dataset the elements of data1 are consecutive dates (2,3,4,5,6 for example) and data2 3,4,5,6,7
so what I am looking for is a line that does not drop to zero
Guillaume
Guillaume 2014년 9월 17일
Please do not post questions in comments but rather start a new question. That way it can be searched and gives someone else the opportunity to get the credit for the answer.
If you don't want to plot the zero, either remove them or replace them with NaN.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by