Analyzing the occurrences of strings within cells using loops
Show older comments
Good afternoon Matlab forums!
I've been working on a code to sort an array of data that includes both numbers and string values. Basically, within column 1 I have identification #'s, in column 2, I have strings, and in column 3 I have charges to be summed.
I am attempting to extract a histogram which shows the occurrences of the strings in column 2 based on particular conditions of the set. Basically, for each unique column 1 ID#(There are multiple occurences of the same here), I wish to check if Column 3 adds up to a particular threshold, in this case being 10,000. Here is a sample of my current code.
Q = cellfun(@str2num,SUB_ID,'UniformOutput',false);
D = cell2mat(Q);
concentated = [D C];
unique_ids = unique(concentated(:,1));
n_ids = length(unique_ids);
for i = 1:n_ids;
A_id = concentated(concentated(:,1) == unique_ids(i),:);
sum_id = sum(A_id(:,2));
if sum_id >= 10000
disp(unique_ids(i))
disp(sum_id)
end
end
The beginning of this code is me creating a 2 column matrix with the data in the array, excluding the previous "Column 2" data, as I can't seem to figure out how to make these loops work within in array as opposed to a matrix. Any help would be appreciated!!!
Thanks, J
7 Comments
dpb
on 12 Oct 2015
How about a sample (short) data file illustrating precisely what you have and how it's stored and then the desired output for that dataset...I'm having trouble parsing exactly what you're looking for.
Julian Cruz
on 12 Oct 2015
Edited: dpb
on 12 Oct 2015
dpb
on 12 Oct 2015
OK, that helps and thanks for the short data set...but, it would be good to have some that represent the general case and how they're stored. How about attaching a set of 100 or so so that the condition would be true and would be able to have a set of providers over which could try the histogram idea.
I think I'd approach the first part a little differently, though--for your set in an array x
>> [u,~,iu]=unique(x(:,1)); % unique IDs, index
>> s=accumarray(iu,x(:,3)) % compute sum by ID
s =
665
450
>> ix=s>500; % check against threshold--logical address
>> [u(ix) s(ix)] % result for those above threshold
ans =
2482720 665
>>
Note I used $500 as the threshold to have a positive result; you want $10k but it's trivial to set at any level this way, even after the fact it's just screening the result already obtained for each ID. I'm pretty sure there's a way to handle the other--I could make up some data but it'd be better to have the real mccoy...
Julian Cruz
on 13 Oct 2015
Edited: Julian Cruz
on 13 Oct 2015
dpb
on 13 Oct 2015
Well, bummer! There's a but in Firefox that it renders the ASCII file content as text for download links from the Forum. I had not uprgraded and had a workaround w/ the earlier release but last night I accidentally missed the 'cancel' on the nag screen and the upgrade happened. I can't get to the .xlsx file from here (not your fault; it's the browser here and I don't have another installed). How about attaching a text copy of the data within the spreadsheet? I can get to that, I think...or, use my contact info at the Contributors page and just e-mail me the file itself and I'll try to get a chance to look later on...
Walter Roberson
on 13 Oct 2015
Right click and Save Link As, and then open the downloaded file.
dpb
on 13 Oct 2015
As noted in the other thread, that worked w/ the earlier release; it's not working this AM after the upgrade--tried several incantations/variations with no success, unfortunately.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!