Dimensions of matrices being concatenated are not consistent.

91 ビュー (過去 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 Center および File ExchangeArithmetic Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by