Keep getting error in splitapply

8 Ansichten (letzte 30 Tage)
JFz am 9 Mär. 2017
Kommentiert: JFz am 16 Mär. 2017
I keep getting this error but I checked my code again and again and can't find anything.
Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in splitapply>localapply (line 250) finalOut{curVar} = vertcat(funOut{:,curVar});
Error in splitapply (line 133) varargout = localapply(fun,splitData,gdim,nargout);
Could anyone tell me why this is happening?
Jennfier
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Gillian Rosen am 15 Mär. 2017
Hi Jennifer,
I understand that when you try to run your code, you are encountering an error related to the 'splitapply' function.
If you are using the 'splitapply' function with the 'mean' or 'sum' functions, then you may be encountering this error due to the way 'mean' and 'sum' work differently for matrices vs. vectors. To use 'mean' as an example, it computes the column-wise average for matrices:
>> mean([1 1; 1 3])
ans =
1     2
But it computes the overall average for vectors:
>> mean([2 4])
ans =
3
These results cannot be vertically concatenated. Therefore, if you try using 'mean' with 'splitapply', such as:
>> a = [1 1; 1 3; 2 4];
>> b = [1; 1; 2];
>> splitapply(@mean, a, b);  % NOT recommended usage
You will encounter the error you described ('Error using vertcat...'). To avoid this error, you can specify that 'mean' should return the mean along rows instead of columns. You can do this by using a slightly different function handle:
>> splitapply(@(x)mean(x, 1), a, b);
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
JFz am 16 Mär. 2017
Thank you!

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by