Deleting some columns of matrix and obtain the original column index.

3 ビュー (過去 30 日間)
Wan-Yi Chiu
Wan-Yi Chiu 2018 年 10 月 24 日
編集済み: Wan-Yi Chiu 2018 年 10 月 24 日
Dear friends
I need really some help/advice. My problem is stated as follows.
The original data is a X=100*50 matrix. The dependent variable is a y=100*1 vector.
Step 1: I regress y on X and get the regression coefficient "b". (The statistic toolbox)
Step 2: Sorting the coefficient "b" and get the index "I".
Step 3: Delete the first column denoted by "I" and get the new matrix X.
Step 4: Repeat the procedures Step 1 -Step 3 20 times until the new matrix X contains only 100*30.
Step 5: How can use the for loop to get the original index of the new matrix X?
For example, let X be a 9*5 matrix and y=ones(9,1). My codes for "2 runs" are as follows:
X=[4 8 11 10 14; 12 9 8 12 13; 10 5 7 10 13;
25 16 5 7 7 ; 12 7 8 17 12; 18 15 13 18 31;
7 5 4 35 16; 6 14 10 15 4 ; 8 11 8 11 10]
Step 1:
[b,bint,r] = regress(y,X); %regress y on X, where b is the regression coefficient (Statistic Toolbox)
Step 2:
[B, I]=sortrows(b) %I is the original column index, in this case, I=[2 5 4 1 3]
Step 3:
X(:,2)=[] %Delete the column 2 of old X after sorting b
Step 4:
[b,bint,r] = regress(y,X); %regress y on X again
Step 5:
[B, I]=sortrows(b) %I is the new column index, in this case, I=[4 3 1 2]
Step 6:
X(:,4)=[] % Delete the column 4 of new X
Step 7: Thus, the new matrix contains only the columns 1,3,4 of the original matrix.
How can I get original index of X after deleting? In this case, just output [1 3 4]
What I need the code to do is the following:

採用された回答

KSSV
KSSV 2018 年 10 月 24 日
A = rand(100,50) ; % some random data
N = 20 ;
C = zeros(N,1) ;
for i = 1:N
[m,n] = size(A) ;
b = rand(n,1) ; % some random coefficients
[B,idx] = sort(b) ;
% remove the column
A(:,idx(1)) = [] ;
% save thew removed column
C(i) = idx(1) ;
end
% get the remained columns indics
iwant = setdiff(1:size(A,2),C) ;

その他の回答 (1 件)

Wan-Yi Chiu
Wan-Yi Chiu 2018 年 10 月 24 日
編集済み: Wan-Yi Chiu 2018 年 10 月 24 日
Thank you very much.
I have tried the codes offered.
However, in my case, I can not get the array [1 3 4]
I mean that: If the original matrix has 5 columns.
The first operation I remove the column 2 from the original matrix.
The second operation I remove the column 4 from the new matrix.
However, this column should be labelled by the column 5 from the original matrix.
Thus, the last matrix left after removing contains only the array [1 3 4] of the original matrix.
The answer should be [1 3 4] that is the original index of the matrix.
It is kind of you to check it again.

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by