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

3 次查看(过去 30 天)
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?

采纳的回答

dpb
dpb 2022-9-6
编辑:dpb 2022-9-6
[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 个评论
dpb
dpb 2022-9-8
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! :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by