How to do matching in table format?

7 Ansichten (letzte 30 Tage)
SC
SC am 17 Aug. 2018
Beantwortet: Peter Perkins am 24 Aug. 2018
Hi,
I have some data, putting in a matrix A:-
Col1 = [1;4;0];
Col2 = [2;5;2];
Col3 = [3;6;4];
A = [Col1,Col2,Col3]
I want to extract the rows where column 2 have the value 2 (i.e. c), and also have another output with those values being updated from 2 to 200 (i.e. d).
b=(A(:,2)==2);
c=A(b,:)
d=A;
d(b,2)=200
However, now the data I obtained is a table, instead of a matrix:-
A2 = table(Col1,Col2,Col3)
I tried to use A2 to replace A in the above codes and try to obtain c and d, but I failed.
How can I obtain c and d? Many thanks!
  1 Kommentar
dpb
dpb am 18 Aug. 2018
If the operations on the data are more convenient as array rather than as separate variables, then create the table variable as an array instead --
A2=table(A);
If there are specific reasons for keeping separate columns because most is column/variable related but there's the occasional array operation, you can retrieve the desired columns as array--
A2=table(Col1,Col2,Col3);
a=A{:,1:3};
and a is the array equivalent to A above to operate on and then put result back into the table.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Peter Perkins
Peter Perkins am 24 Aug. 2018
b = (A2.col2 == 2); % or A2{:,2) == 2
c = A(b,:) % leave this as a table
d = A2;
d.Col2(b) = 200; % or d{b,2} = 200
There's quite a lot of documentation about table subscripting that will explain all this.

Weitere Antworten (0)

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by