필터 지우기
필터 지우기

Remove columns from a matrix with a loop.

조회 수: 6 (최근 30일)
daniel.x16
daniel.x16 2011년 6월 8일
Hi, I know I posted this a couple of days ago, and the info really helped, this is just a continuation of the question with my code posted.
I am graphing data from different databases, and sometimes the data is missing. In this case, instead of a double array, the data is a string 'No Data'. I am writing a loop to delete columns with this string in place of the data. I need to delete the column from the dataarray that holds the data, and the namearray that holds the names of the data. They are both 1 by n cell arrays.
What I have now:
for i=1:length(dataarray) %Length of the data array
if strcmp(dataarray{i},'No Data')
%Sees if the column of the data array has no data
namearray(:,i)=[];
%Deletes the name for which there is no data
dataarray(:,i)=[];
%Deletes the column that has no data
end
end
Now this works like a charm when there is one column that has no data. However, it fails when there is more than one.
I get: ??? Index exceeds matrix dimensions.
Error in ==>>
If strcmp(dataarray{i},'No Data'}
I am not really sure why I am getting this error. I know with loops each iteration moves the reference frame of the number deleted. (So like if the first column is deleted on the first repetition of the loop, now the second column becomes the first column).
Thanks for any help! I have been playing with this all morning, and I am not sure the best way to accomplish this is.

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 6월 8일
Once you delete a column your matrix is smaller so the last entry (length(dataarray)) no longer exists.
The easiest remediation for this is to run it backwards!
It would still be best if you create an index list of all of the columns you need to remove and then do it all at once. E.g. something like:
idx = cellfun(@(x)strcmp(x,'No Data'),dataarray);
dataarray(:,idx) = [];
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2011년 6월 8일
length(dataarray):-1:1 %negative 1 increment
daniel.x16
daniel.x16 2011년 6월 8일
Thanks so much!

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

추가 답변 (2개)

Matt Fig
Matt Fig 2011년 6월 8일
Quoting myself (although the quotes really mess up a post!) from your last question:
The reason why you should not use a FOR loop for this is that you set the loop to iterate over the original length of the variable. Then if you delete a value in the variable, the length of the variable decreases, but the loop will keep right on going.
Did you read this? Did you try the example I gave and think about the problem???
Use CELLFUN.
DA = {'No Data',magic(3),'No Data',[1 2 3],[6 6 6 6],'No Data'};
idx = cellfun(@(x) strcmp(x,'No Data'),DA);
DA = DA(~idx)
  댓글 수: 2
daniel.x16
daniel.x16 2011년 6월 8일
I thought that was what you recomeneded. Instead of going from 1:n, I went from 1:length(array).
daniel.x16
daniel.x16 2011년 6월 8일
Nevermind...I read it through more carefully. That makes sense.

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


Fangjun Jiang
Fangjun Jiang 2011년 6월 8일
You ignored the caution both Matt and I gave you in your previous question. You should not do it in the loop.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by