Extract element from column vector with condition

조회 수: 6 (최근 30일)
Sagar Dhage
Sagar Dhage 2014년 8월 8일
댓글: Sagar Dhage 2014년 8월 22일
I have column vector
P=[ 0; 212; 111; 190; 0; 223; 123; 200; 189; 219; 0; 190; 175; 202; 167; 181
....and so on]
I want to extract the 2nd and 3rd elements from the left 0 element IF LENGTH BETWEEN TWO 0 (the left 0 and right 0) is GREATER THAN 3. Save 2nd position elements in P2 column vector and 3rd position elements in P3 column vector. The result should be
After extraction, the remaining elements should be placed into P1 such as
P1 = [ 0; 212; 111; 190; 0; 223; 189; 219; 0; 190; 167; 181 ....and so on]
P2 = [123; 175]
P3 = [200; 202]
  댓글 수: 7
Sagar Dhage
Sagar Dhage 2014년 8월 8일
Oh sorry, extract 2nd and 3rd position element if IF LENGTH BETWEEN two 0 is GREATER THAN 3.
Image Analyst
Image Analyst 2014년 8월 8일
편집: Image Analyst 2014년 8월 8일
It might be clearer if you said "is 4 or more" rather than is "greater than 3". So the left run has 121, 111, 190 and that's 3, but not more than 3 (not 4 or greater) so those are left alone and get transferred to P1.
It's fairly easy if you have the Image Processing Toolbox. Do you?

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

채택된 답변

Nir Rattner
Nir Rattner 2014년 8월 8일
편집: Nir Rattner 2014년 8월 8일
You can use the “find” function to find the indices of your zeros and then subtract each index by the one before to make sure the distance between them is not too large.
P=[ 0; 212; 111; 190; 0; 223; 123; 200; 189; 219; 0; 190; 175; 202; 167; 181; 0;];
Pzeros = find(P == 0);
violators = Pzeros(find(Pzeros(2 : end) - Pzeros(1: end - 1) > 4));
P1 = P;
P1([violators + 2; violators + 3]) = [];
P2 = P(violators + 2);
P3 = P(violators + 3);

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 8일
편집: Azzi Abdelmalek 2014년 8월 8일
Edit
P=[ 0; 212; 111; 190; 0; 223; 123; 200; 189; 219; 0; 190; 175; 202; 167; 181 ]
ii=strfind(P'~=0,[0 1])
idx=ii([diff([ii numel(P)])]>4)
P1=P'
jj=[idx+2 idx+3]
P1(jj)=[]
P2=P(idx+2)'
P3=P(idx+3)'

Image Analyst
Image Analyst 2014년 8월 8일
Method using the Image Processing Toolbox:
P=[ 0; 212; 111; 190; 0; 223; 123; 200; 189; 219; 0; 190; 175; 202; 167; 181]
P2 = [];
P3 = [];
measurements = regionprops(P~=0, P, 'PixelValues', 'Area')
for k = 1 : length(measurements)
% Show length of this stretch in the command window:
fprintf('Area of stretch #%d = %d\n', k, measurements(k).Area);
% Tack onto P2 and P3 if the length >= 4.
if measurements(k).Area >= 4
P2 = [P2, measurements(k).PixelValues(2)];
P3 = [P3, measurements(k).PixelValues(3)];
end
end
% Show in command window:
P2
P3
  댓글 수: 3
Image Analyst
Image Analyst 2014년 8월 12일
I'm leaving on a week long trip in an hour. It looks like you've accepted an answer so then just go with that.
Sagar Dhage
Sagar Dhage 2014년 8월 22일
I have modified your programme.. nw its gives wat I want..Thank you.
% measurementsX = regionprops(Px~=0, Px, 'PixelValues', 'Area') % for k = 1 : length(measurementsX)
for k = 1 : length(measurementsX) % Show length of this stretch in the command window: %fprintf('Area of stretch #%d = %d\n', k, measurementsX(k).Area); % Tack onto P2 and P3 if the length >= 4. if measurementsX(k).Area == 1 P1X_1 = [P1X_1, measurementsX(k).PixelValues(1)]; end end
for k = 1 : length(measurementsX) % Show length of this stretch in the command window: %fprintf('Area of stretch #%d = %d\n', k, measurementsX(k).Area); % Tack onto P2 and P3 if the length >= 4. if measurementsX(k).Area == 3 P1X_3 = [P1X_3, measurementsX(k).PixelValues(1)]; P2X_3 = [P2X_3, measurementsX(k).PixelValues(2)]; P3X_3 = [P3X_3, measurementsX(k).PixelValues(3)]; end end
for k = 1 : length(measurementsX) % Show length of this stretch in the command window: %fprintf('Area of stretch #%d = %d\n', k, measurementsX(k).Area); % Tack onto P2 and P3 if the length >= 4. if measurementsX(k).Area ==5 P1X_5 = [P1X_5, measurementsX(k).PixelValues(1)]; P4X_5 = [P4X_5, measurementsX(k).PixelValues(2)]; P5X_5 = [P5X_5, measurementsX(k).PixelValues(3)]; P2X_5 = [P2X_5, measurementsX(k).PixelValues(4)]; P3X_5 = [P3X_5, measurementsX(k).PixelValues(5)]; end end

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by