Plotting specific values of 2xn array based on values in first row

조회 수: 3 (최근 30일)
Zenid
Zenid 2015년 9월 22일
답변: Rose 2015년 9월 22일
Hello! I have 2xn arrays. The first row contains values between 1 and 6 that code specific events that happened at the timepoints that are saved in the second row. Something like this:
1 4 5 1 5
200 350 360 400 600
Goes on like that. It's probably really basic. I want to plot only the timepoints in the second row for the specified event codes in the first row. I hope this is clear. Thanks!

채택된 답변

Sebastian Castro
Sebastian Castro 2015년 9월 22일
편집: Sebastian Castro 2015년 9월 22일
You want to be looking at row 1 for every index that matches a certain value... let's take 5 for this example.
row1 = x(1,:);
indices = (row1 == 5);
Then, you want to use those indices to index into row 2:
row2 = x(2,:);
output = row2(indices);
Now, this has a lot of intermediate variables, so you might instead want to do it all in one shot:
output = x(2,x(1,:)==5);
- Sebastian

추가 답변 (1개)

Rose
Rose 2015년 9월 22일
I think you mean like this:
>> x=[1 4 5 1 5;200 350 360 400 600];
>> plot(x(1,:),x(2,:))
You even can add more details to the plot or set the axis limits:
>> plot(x(2,:),x(1,:),'*','LineWidth',1);
>> axis([100 700 0 6]);
xlabel('Time')
ylabel('Values')

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by