How do I use a for loop to index a categorical vector?

6 views (last 30 days)
Here is my code:
hydrocephalus_1 = ismember(DRG, {'31', '32', '33'});
idx = find(hydrocephalus_1 == 1);
icdpcs_values = zeros(length(idx), 1);
for i = 1:length(idx)
icdpcs_values(i) = i10_pr1(idx(i));
i = i+1;
end
[GC, GR] = groupcounts(icdpcs_values)
i10_pr1 is a categorical vector. I am trying to see the values in i10_pr1 that exist at the locations of 31, 32, and 33 in DRG. The result is coming out as different values than are in i10_pr1. The values that are coming out are just numbers when they should be a mix of numbers and letters. Is this issue arising because i10_pr1 is a categorical vector? Can someone help debug this so that it runs properly? Thanks for the help!!
  3 Comments
caroline lesniak
caroline lesniak on 7 Jul 2020
I cannot attached the data because it is confidential, but the DRG vector is correlated 1:1 with i10_pr1. They are different columns from the same data table.
dpb
dpb on 7 Jul 2020
Throw some random values in in place of the real thing then. But somehow need to be able to have a test case that reproduces your issue.

Sign in to comment.

Accepted Answer

dpb
dpb on 7 Jul 2020
If the two indeed correlate, the result should be as expected -- ergo, can only presume they don't correlate as you think...example that simulates your code:
DRG=randi(40,20,1); % a sample for DRG
i10_pr1=categorical(cellstr(char('A'+[0:19]'))); % a categorical variable to carry along with DRG
>> tCL=table(DRG,i10_pr1) % create a table from both variables
tCL =
20×2 table
DRG i10_pr1
___ _______
38 A
20 B
20 C
14 D
37 E
15 F
5 G
32 H
16 I
10 J
17 K
4 L
6 M
38 N
39 O
24 P
3 Q
10 R
15 S
33 T
>> tCL(ismember(tCL.DRG,[31,32,33]),:) % the outputs for desired/looked-for rows
ans =
2×2 table
DRG i10_pr1
___ _______
32 H
33 T
>> tCL(ismember(tCL.DRG,[31,32,33]),'i10_pr1') % return the categorical variable only
ans =
2×1 table
i10_pr1
_______
H
T
>>
I used unique categories above to make identifiable as being the specific rows in question.
As noted above, to produce something that replicates what you think is an error will require having a test/sample case that illustrates the problem; no way to know how to reproduce your symptoms without being able to see what is really going on.
  3 Comments
caroline lesniak
caroline lesniak on 7 Jul 2020
I figured it out thank you for the help!! This was very helpful. I just had to convert [31, 32, 33] to a categorical array.
dpb
dpb on 7 Jul 2020
OK. Knew it had to be something straightforward but not being able to see your terminal from here makes debugging somewhat more difficult...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!