필터 지우기
필터 지우기

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

조회 수: 2 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by