Remove certain elements from a matrix

조회 수: 11 (최근 30일)
NT
NT 2016년 4월 15일
답변: Roger Stafford 2016년 4월 16일
I have a text file Pressure which has a lot of rows and two columns and I want to remove the rows and columns where the columns are equal to zero, and display the old graph and new graph on the same plot and this is what i have so far, I don't know what to put in the if statement.
% Copy data from Pressure to OldData
OldData = load('Pressure.txt');
% Copy OldData to NewData
NewData = OldData;
% Remove points where there are zeros
for i = 1:length(OldData)
if(OldData(i,2) == 0)
end
end
% Plot both in same plot:
plot(OldData(:,1),OldData(:,2),'--b',NewData(:,1),NewData(:,2), 'r')
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 15일
Make your Question clear. Avoid details that are not important to resolve your problem. You can post a short example with expected result

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

답변 (2개)

James Tursa
James Tursa 2016년 4월 15일
편집: James Tursa 2016년 4월 15일
Replace this:
% Remove points where there are zeros
for i = 1:length(OldData)
if(OldData(i,2) == 0)
end
end
with this:
% Remove points where there are zeros
x = OldData(:,2)==0; % Logical indexes where column 2 is 0
NewData(x,:) = []; % Remove the rows where column 2 is 0

Roger Stafford
Roger Stafford 2016년 4월 16일
Or you just write:
NewData = OldData(OldData(:,2)==0,:);

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by