Error using == Matrix dimensions must agree.

조회 수: 1 (최근 30일)
Tim Johansson
Tim Johansson 2020년 11월 11일
댓글: Tim Johansson 2020년 11월 11일
Im trying to remove bad epochs from a dataset.
I've previously substituted every bad epoch with [] inside a cell array, looking like this:
The cell array is called combine1 and is a 400x1 cell array.
i use the following code to try and remove the empty cell array elements for good
for i = 1:length(combined1)
if combined1{i} ==[]
combined1(i)=[]
else
end
end
however i get the error:
"Error using ==
Matrix dimensions must agree."
Sadly i cant change the previous preprocessing of the data into the cell array, so i need a way to change this array.
Anyone who can spot the mistake?

채택된 답변

Stephen23
Stephen23 2020년 11월 11일
편집: Stephen23 2020년 11월 11일
You don't need a loop, try this:
idx = cellfun(@isempty,combined1);
combined1(idx) = []
"Anyone who can spot the mistake?"
You should have used isempty instead of comparing two matrices with incompatible sizes. Array size compatibility is explained here:
In short, arrays are compatible if for every dimension one of these is true:
  • one of the arrays is scalar size for that dimension
  • both of the arrays have the same size for that dimension.
Consider the two arrays that you are comparing: one has size 62x769, the other has size 0x0. Are they compatible? (hint: no)
You would also run into problems because you are trying to remove array elements from the array that you are iterating over. There are two common ways to avoid this:
  1. create a logical mask and remove the elements after the loop,
  2. loop over the elements backwards.
  댓글 수: 1
Tim Johansson
Tim Johansson 2020년 11월 11일
Thanks that solved it
And the explanation was very helpful as well.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by