Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's solve this step by step: I'm assuming <code>FIT_txt</code> is a <code>Nx1</code> cell array and <code>sector</code> is a <code>Mx1</code> cell array.</p> <p>First find the rows in FIT_txt where <code>sector(ii)</code> appears in, and also how many times:</p> <pre><code>ii=1; sector_occurrence = strfind(FIT_txt,sector(ii)); </code></pre> <p>This gives you <code>N</code> cells for each row of FIT_txt with in each cell the actual location at each row where <code>sector(ii)</code> is found. You just need the count per row, so use <a href="http://www.mathworks.nl/help/techdoc/ref/numel.html" rel="nofollow">numel</a> to get just that:</p> <pre><code>cellfun(@numel,sector_occurrence ); </code></pre> <p>You can do this for every element of <code>sector</code> with a loop or with <a href="http://www.mathworks.nl/help/techdoc/ref/arrayfun.html" rel="nofollow">arrayfun</a>:</p> <pre><code>sector_occurrence = cell2mat(arrayfun(@(ii) cellfun(@numel,strfind(FIT_txt,sector{ii})),1:numel(sector),'uni',false')); </code></pre> <p>Now you have a NxM matrix. The element at row i and column j tells how many times <code>sector(j)</code> occurs in <code>FIT_txt(i)</code>.</p> <p>*reading your comments... Aah, but if a sector is found, FIT_txt is exactly equal to the sector, which allows you the simplify all the above this with strcmp and arrayfun:</p> <pre><code>sector_occurrence = cell2mat(arrayfun(@(sectorii) strcmp(FIT_txt,sectorii), sector', 'uni',false)) </code></pre> <p><br><br> Now onto the summation:</p> <p>It's just a vector product of the occurrence vector with <code>FIT_num</code>, and you can get them all in one call by multiplying the <code>sector_occurrence</code> matrix with <code>FIT_num</code>:</p> <pre><code>sumresult = sector_occurrence'*FIT_num; </code></pre> <p>note the transpose operator <code>'</code> because <code>sector_occurrence</code> was defined as <code>NxM</code>.</p> <h2>Example</h2> <pre><code>&gt;&gt; sector=[{'AB10'} ; {'b'}]; &gt;&gt; FIT_txt=[{'AB10'} ; {'a'} ; {'b'} ; {'ZX5b'} ; {'AB10'} ; {'b'}]; &gt;&gt; FIT_num = (1:6)'; &gt;&gt; sector_occurrence = strfind(FIT_txt,sector(ii)) sector_occurrence = 1 0 0 0 0 1 0 0 1 0 0 1 &gt;&gt; sumresult = sector_occurrence'*FIT_num sumresult = 6 9 </code></pre> <p>Of course, it has to be an exact match (which you said it was) <code>b</code> won't be equal to <code>B</code> and also not to <code>bb</code>, only <code>b</code>.</p> <h2>Remark on large data set</h2> <p><code>sector_occurrence</code> is a <code>logical</code> array, so it doesn't need that much memory. But when executing the multiplication with <code>FIT_num</code>, it gets converted to <code>float</code>, which needs 8 times as much space. The end result is a small (in memory) vector, but the intermediary process can melt your pc. You can avoid this by doing the multiplication in a loop:</p> <pre><code>sumresult=NaN(numel(sector),1); %preallocation is a good thing for ii=1:numel(sector) sumresult(ii)=sector_occurrence(:,ii)'*FIT_num; end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload