create a new matrix from a vector

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 个评论

Why do you think transpose() should not be used?
My data was in that form, so, i was asking if i could do it directly.
It works with newV also....you need not to change any part of code.

请先登录,再进行评论。

 采纳的回答

R=arrayfun(@(x)reshape(newV,1,[])==u(x),1:numel(u),'un',0);

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by