How to return variables from table?

3 vues (au cours des 30 derniers jours)
Paulius Vaikasas
Paulius Vaikasas le 14 Juil 2015
Commenté : Steven Lord le 11 Août 2015
Hello, this question might seem little bit sily. Here is the thing I have table with data (5 columns), I want to return not only rows in which logical funcionts says 1 but and precending and next rows. Could someone tell me how to do that, thanks.

Réponse acceptée

the cyclist
the cyclist le 14 Juil 2015
You could do something like this:
(1) Identify the rows that match.
idx1 = find(ismember(tableName.variableName,[1 2 3])); % Apply your logic here.
(2) Get the neighboring rows
idx2 = unique([idx1; idx1-1; idx1+1])
(3) That step might get you row number zero, and a row beyond the size of the table, so trim those
idx2 = max(1,idx2);
idx2 = min(size(tableName,1),idx2);
idx2 = unique(idx2); % Might have double-counted the first and last row, so get rid of those. (Could do this better)
  2 commentaires
Paulius Vaikasas
Paulius Vaikasas le 11 Août 2015
Modifié(e) : Paulius Vaikasas le 11 Août 2015
Thank you!
Steven Lord
Steven Lord le 11 Août 2015
Don't FIND. OR.
x = randperm(20);
y = x > 15;
before = [false, y(1:end-1)]; % No element before the first
after = [y(2:end) false]; % No element after the last
[x; y | before | after]
Elements of x that are greater than 15 or that are immediately before or after an element greater than 15 will have a 1 in the corresponding element of y. You can replace the definition of y with something else:
y = ismember(x, [3 18 7]);
If your x is a column vector, you would need to tweak this slightly to make before and after columns not rows.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by