create a new matrix from a vector

1 回表示 (過去 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 Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by