How would I go about graphing columns filtered by specific results in a third column?
The following table as an example, how would I plot only the second and third column entries that are 1.0000 in the first?
A =
1.0000 1.0000 5.5000
1.0000 2.0000 1.2000
0 3.0000 6.4000
1.0000 4.0000 3.7000
0 5.0000 3.6000
0 6.0000 9.6000

댓글 수: 1

Dyuman Joshi
Dyuman Joshi 2023년 7월 19일
편집: Dyuman Joshi 2023년 7월 19일
Try comparing with the first column values with 1.000, ideally with a tolerance, and use logical indexing to get the corresponding values in 2nd and 3rd column.

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

 채택된 답변

Jon
Jon 2023년 7월 19일

0 개 추천

So to make @Dyuman Joshi suggestion more concrete
% Define example points
A = [
1.0000 1.0000 5.5000
1.0000 2.0000 1.2000
0 3.0000 6.4000
1.0000 4.0000 3.7000
0 5.0000 3.6000
0 6.0000 9.6000];
% Define tolerance for finding rows to plot
tol = 1e-6;
% plot rows meeting critiera that element in first column is a 1
idl = ismembertol(A(:,1),1,tol); % or alternatively idl = abs(A(:,1)-1)<=tol
plot(A(idl,2),A(idl,3))

댓글 수: 8

Jon
Jon 2023년 7월 21일
Did this answer your question?
Brandon
Brandon 2023년 7월 21일
Would this method also work if I'm trying to filter it by a string?
If you are filtering by a string then I assume the match would be exact, no tolerance involved. Also you couldn't store the numerical data and the strings in a double array, you would have to use some other structure, like a cell array. Here are some ways you could filter by a string:
% Define example points
A = {
'ABC' 1.0000 5.5000
'ABC' 2.0000 1.2000
'DEF' 3.0000 6.4000
'ABC' 4.0000 3.7000
'XYZ' 5.0000 3.6000
'PQT' 6.0000 9.6000};
% plot rows meeting critiera that element in first column is 'ABC'
idl = strcmp(A(:,1),'ABC');
figure
plot([A{idl,2}],[A{idl,3}]); % use [] to converts comma separated list to vector
% Or maybe a clearer way to do this, is to use a table
T = cell2table(A); % columns are named by default as A1,A2,A3
idl = strcmp(T.A1,'ABC');
figure
plot(T.A2(idl),T.A3(idl))
Brandon
Brandon 2023년 8월 2일
Quick followup, say I wanted to filter the first column by elements that are either 1 or 2 using the idx and {:,1} method, what would that look like?
If you only want to plot rows where the element in the first column is either a 1 or a 2, then there are no tolerances involved. For this you can use basic logical indexing.
% Define example points
A = [
4 1.0000 5.5000
1 2.0000 1.2000
2 3.0000 6.4000
3 4.0000 3.7000
0 5.0000 3.6000
2 6.0000 9.6000];
% Plot rows meeting critiera that element in first column is a 1 or a 2
idl = A(:,1)==1 | A(:,1)==2
idl = 6×1 logical array
0 1 1 0 0 1
plot(A(idl,2),A(idl,3),'o-')
Brandon
Brandon 2023년 8월 2일
Got it, thanks. I asked because I had tried that but using || instead of | since I'm used to java haha
Dyuman Joshi
Dyuman Joshi 2023년 8월 2일
You can also use ismember for that.
Yes, that's cleaner and connects back to the beginning of the the thread where the match was approximate and we had to use ismembertol.
% Define example points
A = [
4 1.0000 5.5000
1 2.0000 1.2000
2 3.0000 6.4000
3 4.0000 3.7000
0 5.0000 3.6000
2 6.0000 9.6000];
% Plot rows meeting critiera that element in first column is a 1 or a 2
idl = ismember(A(:,1),[1,2])
idl = 6×1 logical array
0 1 1 0 0 1
plot(A(idl,2),A(idl,3),'o-')

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2023년 7월 19일

댓글:

Jon
2023년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by