create a new matrix from a vector

조회 수: 18 (최근 30일)
Elysi Cochin
Elysi Cochin 2019년 3월 13일
댓글: madhan ravi 2019년 3월 13일
create a new matrix from
v = [1 1 1 2 2 2 3 3 4];
u=unique(v);
R=arrayfun(@(x)v==u(x),1:numel(u),'un',0);
M=+vertcat(R{:})
what if my matrix is
newV = [1; 1; 1; 2; 2; 2; 3; 3; 4];
what is the change i need to make without transposing the vector newv, to get the output M with newV
i used horzcat, but still without transposing, i'm not able to get result as M
  댓글 수: 3
Elysi Cochin
Elysi Cochin 2019년 3월 13일
My data was in that form, so, i was asking if i could do it directly.
KSSV
KSSV 2019년 3월 13일
It works with newV also....you need not to change any part of code.

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2019년 3월 13일
R=arrayfun(@(x)reshape(newV,1,[])==u(x),1:numel(u),'un',0);
  댓글 수: 1
madhan ravi
madhan ravi 2019년 3월 13일
Alternative:
+(reshape(newV,1,[])==unique(newV))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by