Filter löschen
Filter löschen

How to filter a table in matlab and then assign a value using a loop

3 Ansichten (letzte 30 Tage)
Hi. I have a table A with 5 columns, the first three columns are categorical and the other two are numbers. The table contains 198 riws and is like this:
TumoSize NodalStatus Grading ER HER2
pT1b pN0 G2 0 0
pT2 pN3 G3 90 3
pT2 p1mi G2 55 2
...
Now, I have to filter this table per raw and assign a score of:
  • 2 when TumorSize is equal to pT2 or pT3 or pT4
  • 1 when NodalStatus is equal to pN2 or N3
  • 1 when Grading is equal to G3
  • 1 when ER is <70
  • 1 when HER2 is equal to 0 or 1 or 2
At the end, I have to sum all this score per raw and then divide in three groups, according to the score (score between 0-2; score 3 or 4 and score 5-7)
My code, that doesn't work, is:
for r = 1:size(A,1)
for c = 1:size(A,2)
g = A(r,c);
score = 0;
if g == 'pT2' || g == 'pT3' || g == 'pT4' || g == 'pN2' || g == 'pN3'
score = 2;
else if g == 'G3' || g >= 0 && g<70 || g == 0 || g == 1 || g == 2
score = 1;
else
score = 0
end
B(r,c) = score
end
end
end
total_score = sum(B,2) %sum score
firstgroup = sum(total_score == 0 | total_score == 1 | total_score == 2)
secondgroup = sum(total_score == 3 | total_score == 4)
thirdgroup = sum(total_score == 5 | total_score == 6 | total_score == 7)
it doesn't work!!! Help me please :)
thank you in advance
  3 Kommentare
Stephan
Stephan am 4 Mär. 2019
Bearbeitet: Stephan am 4 Mär. 2019
I get other results in my answer then Andrei - please feedback...
Also please clarify if correct Nodal Status is N3 or pN3 - since you have the one in your code and the other in your question / description.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephan
Stephan am 4 Mär. 2019
Hi,
try:
% Define a column for Scores
A.Score(1:size(A,1),1) = 0;
% Scores due to Tumor Size
A.Score(A.TumorSize=='pT2' | A.TumorSize=='pT3' | A.TumorSize=='pT4') = A.Score(A.TumorSize=='pT2' | A.TumorSize=='pT3' | A.TumorSize=='pT4') + 2;
% Scores due to Nodal Status
A.Score(A.NodalStatus=='pN2' | A.TumorSize=='pN3') = A.Score(A.NodalStatus=='pN2' | A.TumorSize=='pN3') + 1;
% Scores due to Grading
A.Score(A.Grading=='G3') = A.Score(A.Grading=='G3') + 1;
% Scores due to ER
A.Score(A.ER<70) = A.Score(A.ER<70) + 1;
% Scores due to HER2
A.Score(A.HER2==0 | A.HER2==1 | A.HER2==2) = A.Score(A.HER2==0 | A.HER2==1 | A.HER2==2) + 1;
%Define column for Grouping
A.Group(1:size(A,1),1) = NaN;
% Assign groups due to Scores
A.Group(A.Score>=0 & A.Score<=2) = 1;
A.Group(A.Score>=3 & A.Score<=4) = 2;
A.Group(A.Score>=5 & A.Score<=7) = 3;
Best regards
Stephan

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 4 Mär. 2019
Bearbeitet: Andrei Bobrov am 4 Mär. 2019
Let T - your table
x = sum([ismember(T{:,1:3},categorical({'pT2','pT3','pT4','pN2','N3','G3'})),...
[T.ER < 70 ,ismember(T.HER2,0:2)]],2);
T.group = discretize(x,[0 3 5 7],'categorical',...
{'firstgroup','secondgroup','thirdgroup'});
  5 Kommentare
France
France am 5 Mär. 2019
Thank you Andrei. I get this error
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in Other_solution (line 8)
x = [ismember(A{:,1:3},categorical({'pT2','pT3','pT4','pN2','pN3','G3'})),...
Andrei Bobrov
Andrei Bobrov am 5 Mär. 2019
Hi!
I'm use your mat-file and my code working.
>> load('J:\Octavework\answers\mat-files\A.mat')
>> T = A;
x = [ismember(T{:,1:3},categorical({'pT2','pT3','pT4','pN2','N3','G3'})),...
[T.ER < 70 ,ismember(T.HER2,0:2)]]*[2;ones(size(A,2)-1,1)];
T.group = discretize(x,[0 3 5 7],'categorical',...
{'firstgroup','secondgroup','thirdgroup'})
T =
198×6 table
TumorSize NodalStatus Grading ER HER2 group
__________ ___________ _______ __ ____ ___________
pT1b pN0 G2 0 0 firstgroup
pT2 pN0 G3 90 3 secondgroup
pT2 PN0 G2 0 0 secondgroup
pT1c pN1mi G2 90 0 firstgroup
pT1b pN0 G3 90 2 firstgroup
pT1c pN0 G3 90 1 firstgroup
pT1b pN0 G2 90 0 firstgroup
pT1a pN0 G3 0 3 firstgroup
pT1c pN0 G3 0 0 secondgroup
pT1a pN0 G3 95 0 firstgroup
in live editor:
2019-03-05_22-43-48.png

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by