change output from row to column and display one cell value instead of multiple

5 views (last 30 days)
Hi I have this code:
[locAll,typeAll]=find(mVocs==1)
which is getting index from 1-30 from a matrix (mVocs size x*32) of mostly NaNs and outputting into a 1170*1 matrix x*32 in ascending order. So 1 (for column 1) appears 200 times and then 2, 3, 4 and so on, e.g.
1
1
1
The cell values are either 1,2 or 3. What I want is an output x*33 matrix with the cell value displayed under each column. How do I do this?

Accepted Answer

dpb
dpb on 6 Sep 2022
Edited: dpb on 6 Sep 2022
[locAll,typeAll]=find(mVocs==1)
will return two vectors of the row,column indices (that you've inexplicably called loc/typeAll, respectively, for some unknownst reason).
Oh! Now it dawns on me what it is you want...
RC=accumarray(locAll,typeAll,[],@(v){v.'});
RC will be a cell array of numel(unique(locAll)) x 1 where each cell will have the numel() column indices satisfying the condition for the given row. That could be from empty to size(mVocs,2)
To put into a regular array you would have to allocate the array and augment each cell array to the full length with NaN or other missing indicator. This is fairly straightforward w/ a looping construct or might even be done in a cellfun call.
ADDENDUM/ERRATUM:
I realized I had forgotten to transpose the column vector from column to row to accumulate cell array content being row vectors instead of column vectors. Fixed up above (the ".'" inside the "{}" is the array transpose operator in MATLAB)
  8 Comments
dpb
dpb on 8 Sep 2022
OK, if you have some other indexing operation needed, come back when can identify just what it is you're trying to accomplish.
Oftentimes we can help more if we are told the final/overall problem trying to be solved instead of just being given a particular stumbling block; the tack being taken may be going down the wrong road and approaching the end result from another direction can be all the difference. The easiest problem to solve is the one avoided in the first place! :)

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!