How to alternately exclude raws of a table?

2 visualizaciones (últimos 30 días)
Emerson De Souza
Emerson De Souza el 6 de Nov. de 2011
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

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 6 de Nov. de 2011
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 comentario
Emerson De Souza
Emerson De Souza el 6 de Nov. de 2011
Thank you a lot Fangjun.
Thank you for answering my mistake and suggesting a correction that works perfectly.
Wish you a nice weekend
Emerson

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by