deleting of rows in a matrix

1 visualizzazione (ultimi 30 giorni)
Hello Every one ;
I have an matrix that occurs with time step 0.0004 .And my step end time 0.312.But I want only the datas that 0.004 and multiples of 0.004. How can I write this code
For ex
[0.02 2 8
0.04 3 2
0.08 3 3
0.01 4 6
0.012 5 2
0.014 6 3
0.016 7 2] is existing and
[0.04 2 8
0.08 3 3
0.12 5 2
0.16 7 2] is that ı want

Risposta accettata

Cris LaPierre
Cris LaPierre il 4 Mag 2021
I'd use a logical array (Ch 12 of MATLAB Onramp) created by ismember. I had to fix some values of A.
% Original matrix
A=[0.02 2 8
0.04 3 2
0.08 3 3
0.1 4 6
0.12 5 2
0.14 6 3
0.16 7 2];
% Desired steps
B=(0.04:0.04:0.16);
% Use logical indexing to extract just the desired rows
C = A(ismember(A(:,1),B),:)
C = 4×3
0.0400 3.0000 2.0000 0.0800 3.0000 3.0000 0.1200 5.0000 2.0000 0.1600 7.0000 2.0000
% Alternate - delete the rows that are not wanted
A(~ismember(A(:,1),B),:)=[]
A = 4×3
0.0400 3.0000 2.0000 0.0800 3.0000 3.0000 0.1200 5.0000 2.0000 0.1600 7.0000 2.0000
  5 Commenti
Steven Lord
Steven Lord il 4 Mag 2021
I would use ismembertol instead of ismember to account for floating point arithmetic.
x = 0.1;
y = x + x + x; % 0.3 right?
isPoint3 = y == 0.3 % false, y is close but not exactly 0.3
isPoint3 = logical
0
difference = y - 0.3 % very small
difference = 5.5511e-17
ismember(y, 0.3) % false, not exactly equal
ans = logical
0
ismembertol(y, 0.3) % true, it's close enough
ans = logical
1
Omer Utku Altindag
Omer Utku Altindag il 4 Mag 2021
Now the problem is fixed.Thank you both of you !!!

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by