Dimensions of matrices being concatenated are not consistent.
95 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Cedric
le 15 Sep 2017
Modifié(e) : Cedric
le 15 Sep 2017
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 commentaires
Cedric
le 15 Sep 2017
Modifié(e) : Cedric
le 15 Sep 2017
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 ;-)
Plus de réponses (1)
Image Analyst
le 17 Sep 2017
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 commentaire
Cedric
le 17 Sep 2017
Modifié(e) : Cedric
le 17 Sep 2017
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.
Voir également
Catégories
En savoir plus sur Arithmetic Operations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!