Dimensions of matrices being concatenated are not consistent.

조회 수: 138 (최근 30일)
prashant singh
prashant singh 2017년 9월 15일
편집: Cedric 2017년 9월 17일
I have 1*512 cell and i want it to convert in matrix so i am trying to use cell2mat but i am getting this error :
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 75)
m{n} = cat(2,c{n,:});
i have obtained this cell after using
cellfun(@(x) find(x,1,'first'), cimg,'un',0);
Uploading the mat file for 1*512 as well.
  댓글 수: 2
Adam
Adam 2017년 9월 15일
You haven't shown any code that actually calls cell2mat.
prashant singh
prashant singh 2017년 9월 15일
@Adam just using it after my cellfun.

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

채택된 답변

Cedric
Cedric 2017년 9월 15일
편집: Cedric 2017년 9월 15일
You have empty ( see comments below! ) arrays in your cell array (where FIND found nothing), so the dimensions do not match for a CAT (the content of some cells is 1x1 and the content of others is 1x0).
If you can get rid of these empty arrays (and hence reduce the number of elements in the output), you can do something like:
isEm = cellfun( @isempty, ridx ) ;
data = cell2mat( ridx(~isEm) ) ;
where data contains the 502 "non-empty values". If you need to keep the size of the original data, you can replace empty arrays with e.g. NaNs:
ridx(isEm) = {NaN} ;
data = cell2mat( ridx ) ;
and here data has 512 elements, 10 of which are NaN.
  댓글 수: 5
Cedric
Cedric 2017년 9월 15일
편집: Cedric 2017년 9월 15일
I think that your example should fail in their test suite:
>> {1, [], 3; [], 4, 5; 6 7 []}
ans =
3×3 cell array
[1] [] [3]
[] [4] [5]
[6] [7] []
>> cell2mat( ans )
ans =
1 4 3
6 7 5
unless we invoke gravity, making e.g. the 1 fall down on the 6, as the first dimension is pointing downwards on our screens and there is nothing below the 1 ;-)
Guillaume
Guillaume 2017년 9월 15일
Agreed, it should fail. Reported as a bug to mathworks.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 9월 17일
Try this:
ca = {1, [], 3; [], 4, 5; 6 7 []}
m = [ca{:}]
ca2 = {1, [], [3,4,5]; [], 4, 5; 6 [7,2] []}
m = [ca2{:}]
You'll see
ca =
3×3 cell array
[1] [] [3]
[] [4] [5]
[6] [7] []
m =
1 6 4 7 3 5
ca2 =
3×3 cell array
[1] [] [1×3 double]
[] [ 4] [ 5]
[6] [1×2 double] []
m =
1 6 4 7 2 3 4 5 5
In ca2, you'll note that all arrays are not all the same size, yet it still works.
  댓글 수: 1
Cedric
Cedric 2017년 9월 17일
편집: Cedric 2017년 9월 17일
Hi Image Analyst,
This is normal, : is linear indexing (column first) a 2D cell array, {} makes the output a CSL, and [] is concatenating.
The following legitimately doesn't work:
>> ca3 = {1, [], [3,4,5]; [], 4, 5; 6 [7;2] []} % [7,2] -> [7;2]
ca3 =
3×3 cell array
[1] [] [1×3 double]
[] [ 4] [ 5]
[6] [2×1 double] []
>> m = [ca3{:}]
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
In Guillaume's example,
>> cell2mat({1, [], 3; [], 4, 5; 6 7 []})
ans =
1 4 3
6 7 5
it goes through because there are the same number of non-empty elements in each row/column, when it should not, because the positions of some elements are changed relatively to the other in the output. And if there is a difference in the number of non-empty elements, it doesn't work:
>> cell2mat({1, [], 3; [], 4, 5; 6 7 8})
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 86)
m = cat(2,m{:});
As far as I am concerned, it should not work in the first case, as it goes through by chance when it should fail to indicate an invalid operation.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by