add and remove columns from a marix

I am writing a code , in fact I have a matrix and I select the fist column then I add next column and in each epoch one column will be adding to the previous for example first 1 , the second epoch 1 and 2 , the third epoch 1, 2 and 3 and so on .
I do that like this
for i = 1:size(Matrix,2) new matrix after each iteration = Matrix(:,1:i);
but now before i put end, I would like to evaluate this new matrix.
I would like to say if nnn> 0.5 then keep this and continue the loop otherwise remove this column from the Matrix and go for the next one and continue the loop
Do you have any idea?

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 9월 6일

0 개 추천

n=rand(5,1);
Matrix=magic(5);
NewMatrix=[];
for k=1:size(Matrix,2)
if n(k)>0.5
NewMatrix(:,end+1)=Matrix(:,k);
end
end

댓글 수: 12

Niki
Niki 2011년 9월 6일
In fact, i want to make a regression each time
then i see if the add column can improve i keep it otherwise i remove it.
I would like to say if RMSE< 0.5 then keep this and continue the loop otherwise remove this column from the Matrix and go for the next one and continue the loop
do you think your new like will do that ?
Niki
Niki 2011년 9월 6일
i think it is a good example
[XL,YL,XS,YS,BETA,PCTVAR,MSE,STATS] = PLSREGRESS(X,Y,1)
if MSE <0.5 keep the column and continue the loop
if the MSE >0.5 remove the column from the matrix and continue with the next column
Fangjun Jiang
Fangjun Jiang 2011년 9월 6일
If you want to remove the column, do the follow. Notice the loop from 5 to 1. This is because if you remove the column, the size of the matrix is changed.
n=rand(5,1);
Matrix=magic(5);
for k=size(Matrix,2):-1:1
if n(k)>0.5
Matrix(:,k)=[];
end
end
Niki
Niki 2011년 9월 7일
I would like to have only one column first and then add the next one not have all and then remove one by one in each iteration ,
do you know what I mean?
Fangjun Jiang
Fangjun Jiang 2011년 9월 7일
Then it's the code in the answer.
Niki
Niki 2011년 9월 7일
in fact I did like this
X=rand(10);
y=rand(10,1);
[n,p]=size(X);
RMSECF=[];
for i=p:-1:1;
[xl,yl,xs,ys,beta,pctvar,mse] = plsregress(X(:,1:i),y,1);
if MSE<0.5
X(:,i)=[];
end
end
Fangjun Jiang
Fangjun Jiang 2011년 9월 7일
I can't comment on your solution, because I don't have your plsregress() function. RMSECF is not used. Not sure if mse and MSE mean the same.
Niki
Niki 2011년 9월 7일
the plsregress is a Matlab function, which version do you have?
please just doc it , I revised it
but the mse seems strage, it should be 10 numbers and there is only 4 which I can not recognize them
X=rand(10);
y=rand(10,1);
[n,p]=size(X);
mse=[];
for i=p:-1:1;
[xl,yl,xs,ys,beta,pctvar,mse] = plsregress(X(:,1:i),y,1);
if mse<0.5
X(:,i)=[];
end
end
Fangjun Jiang
Fangjun Jiang 2011년 9월 7일
The function is in the Statistics Toolbox which I don't have.
Niki
Niki 2011년 9월 7일
You can use of following function , please
http://www.mathworks.com/matlabcentral/fileexchange/16465-pls/content/PLSI.m
Fangjun Jiang
Fangjun Jiang 2011년 9월 7일
Sorry, I won't do it. It seems to be a different topic. Ask a different question and someone with statistics expertise might be able to help you.
Niki
Niki 2011년 9월 7일
Thanks Fangiun

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Descriptive Statistics and Insights에 대해 자세히 알아보기

질문:

2011년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by