create a new matrix from a vector

1 view (last 30 days)
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 Comments
Elysi Cochin
Elysi Cochin on 13 Mar 2019
My data was in that form, so, i was asking if i could do it directly.
KSSV
KSSV on 13 Mar 2019
It works with newV also....you need not to change any part of code.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 13 Mar 2019
R=arrayfun(@(x)reshape(newV,1,[])==u(x),1:numel(u),'un',0);
  1 Comment
madhan ravi
madhan ravi on 13 Mar 2019
Alternative:
+(reshape(newV,1,[])==unique(newV))

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!