Table - keeping specific data

9 Ansichten (letzte 30 Tage)
Mark Evans
Mark Evans am 20 Mär. 2021
Bearbeitet: Mark Evans am 23 Mär. 2021
I want a line of code to do the following:
To change all values for weight (column 4) to 1 if it is greater than 5 and 0 if it is less than 5.
Then, to keep only the rows in the table that have as its 3rd column and remove all other data.
This is what I've done so far but it doesnt work becuase: Operator '&' is not supported for operands of type 'categoriclal'
table = readtable("table.csv", opts)
table.Type(table.Type == 1
Thanks in advance.

Antworten (1)

dpb
dpb am 20 Mär. 2021
You've got parentheses in the wrong place to do combination of logical addressing, but unless you only want "1" values in the output array as well as the Type, then that's wrong logic. The logic of your verbal description is
t=table(repmat(ans,10,1),randi(10,20,1),'VariableNames',{'Type','Weight'}); % build sample table
% the engine
t.Weight=1*(t.Weight>=5); % categorize NB neeed either inclusive or not on breakpoint
tSave=t(t.Type=="LAL(L)",:); % save the category wanted.
NB: the note about the breakpoint -- your description if followed literally would leave any weight values of precisely 5 unchanged. I chose to make inclusive; salt to suit.
Also, you asked for "0|1"; that's the "1*" thing--otherwise will return a logical true|false output, not double. Either is equivalent for most purposes.
Also note it's rarely ever needed to actually create the subsidiary dataset; with grouping variables and rowfun one can process by value without the need for the auxiliary temporary variable.
  1 Kommentar
dpb
dpb am 21 Mär. 2021
Works here ok...
>> {'LAL(L)';'LAL(R)'}
ans =
2×1 cell array
{'LAL(L)'}
{'LAL(R)'}
>> t=table(repmat(ans,10,1),randi(10,20,1),'VariableNames',{'Type','Weight'}); % build sample table
>> t.Weight=1*(t.Weight>=5); % categorize NB neeed either inclusive or not on breakpoint
>> tSave=t(t.Type=="LAL(L)",:); % save the category wanted.
>> tSave
tSave =
10×2 table
Type Weight
__________ ______
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
{'LAL(L)'} 0.00
{'LAL(L)'} 0.00
{'LAL(L)'} 1.00
{'LAL(L)'} 1.00
>>
was the exact code I used; I see above I didn't pick up the definition of the string array to build the sample table from, but certainly the "engine" code works for a table with those variables.
Would need to see your actual table and code to diagnose any other problem.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by