Location of value in cell array

1 Ansicht (letzte 30 Tage)
Jared
Jared am 4 Nov. 2011
I have a 2 element cell array:
cell={x y}
x & y are both the same length. I am trying to compare each value in y against a set of user defined inputs, and delete it and its corresponding x if it is outside the range.
lowerbound=str2double(get(handles.min,'String'));
upperbound=str2double(get(handles.max,'String'));
for i=1:length(cell{2})
if cell{2}(i)<lowerbound || cell{2}(i)>upperbound
cell{1}(i)=[];
cell{2}(i)=[];
end
end
However I'm getting Index exceeds matrix dimensions error in the 4th line. I don't see why, but I assume I am misunderstanding on how to address each individal value.

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 4 Nov. 2011
When you delete some of the elements using cell{1}(i)=[], the length of cell{1} reduces thus cause "Index exceeds matrix dimensions error" when i reaches the end.
Use logic index instead and no need to do the for-loop.
index=cell{2}<lowerbound | cell{2}>upperbound;
cell{1}(index)=[];
cell{2}(index)=[];
  5 Kommentare
Jan
Jan am 4 Nov. 2011
The || operator needs scalar inputs. I'd prefer "or()" here.
Fangjun Jiang
Fangjun Jiang am 4 Nov. 2011
@Jan, you are right. My answer is updated.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by