필터 지우기
필터 지우기

How to alternately exclude raws of a table?

조회 수: 2 (최근 30일)
Emerson De Souza
Emerson De Souza 2011년 11월 6일
INTRO: I created a table T using the following command lines:
xvector = 1:1:10;
yvector = 1:3:20;
numX = numel(xvector);
numY = numel(yvector);
yvector = repmat(yvector(:),numX,1);
xvector = repmat(xvector ,numY,1);
COORDINATES= [xvector(:) yvector];
VALUE=(1:1:numX*numY)';
T=[COORDINATES VALUE];
The first two columns are the x and y coordinates and the third is the corresponding value for each x,y-pair.
GOAL: I want to construct a new table TNEW selecting specific elements of T.
So I introduce the parameter SKIP=value (=to skip elements of T). SKIP=1 means all elements of T are used, thus TNEW=T. SKIP=2 means: use every second ELEMENT of T in X AND in Y directions. SKIP=3 means: use every third ELEMENT, and so on.
REMARK: there is a difference between using every n-ten ELEMENT IN X AND Y DIRECTION and not using every n-ten raw along the table: First, x is constant and y runs in arbitrary steps. Thus, for SKIP=4, every fourth raw will be selected until a serie of y is finished (before it starts repeating for a new value of x). Second, the new value of x are also selected in steps of four, thus all values of y for the non-fourth x are also excluded.
To obtain this goal, I wrote the following lines:
SKIP=2;
ix=mod(T(:,1),SKIP)~=0 & mod(T(:,2),SKIP)~=0;
TNEW = T(ix,:);
It works correct for SKIP=1 or SKIP=2, as you can see if you run all the command lines above. PROBLEM: it does not work for SKIP>2 and I don't know how to improve or may be to change it.
I wonder if someone has an idea to achie the same goal for arbitrary n.
Thank you in advance for your attention
Emerson

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 6일
The problem is: the value of x is consecutive natural number, so you can use mod(T(:,1),SKIP) to do the skipping. The value of y is not consecutive so you can not use mod() to implement the skipping. It just happened to match the result when skip==2. If the value of y is different, it won't even work for skip==2.
I am not clear about your problem. It sounds like you want to do the skipping according to the value of x and y. Then you should not over-write xvector and yvector because they keep the value. Use something like this.
SKIP=3;
x_value = 1:1:10;
y_value = 1:3:20;
selected_x_value=x_value(1:SKIP:end);
selected_y_value=y_value(1:SKIP:end);
index=and(ismember(T(:,1),selected_x_value),ismember(T(:,2),selected_y_value));
TNEW=T(index,:)
  댓글 수: 1
Emerson De Souza
Emerson De Souza 2011년 11월 6일
Thank you a lot Fangjun.
Thank you for answering my mistake and suggesting a correction that works perfectly.
Wish you a nice weekend
Emerson

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by