How can I re-arrange a matrix?

1 回表示 (過去 30 日間)
gsourop
gsourop 2020 年 5 月 8 日
コメント済み: gsourop 2020 年 5 月 8 日
Hi everyone,
Let's assume that I have a matrix X, dimensions 10x2, a variable z of dimensions 10x1. Then I want to create an interaction term based on the sorted values of y, so that the first half gets 0 and the highest values get 1. Then I want to regress the interaction term on variable y.
x = randn(10,2);
z = randn(10,1);
y = randn(10,1);
[zsorted,zsindex]=sort(z);
zsorted(1:5)=0; zsorted(6:end)=1;
% Interaction Term
intterm = x(zsindex,:).*zsorted;
How can i transform the elements of the interaction term back to the original order of x? I could get the same results by
res = ols(y(zsindex,:), intterm);
But is there any other way to be done?
Thanks in advance.

採用された回答

Walter Roberson
Walter Roberson 2020 年 5 月 8 日
編集済み: Walter Roberson 2020 年 5 月 8 日
z = randn(10,1);
[~,zsindex]=sort(z);
intterm = z;
intterm(zsindex(1:end/2)) = 0;
  1 件のコメント
gsourop
gsourop 2020 年 5 月 8 日
Many thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by