what am I doing wrong here?

1 Ansicht (letzte 30 Tage)
Jonathan Trencheny
Jonathan Trencheny am 15 Feb. 2016
Beantwortet: Star Strider am 15 Feb. 2016
function a = problem4c (m)
m=4;
a = [1:m; 2:m; 3:m; 4:m; 5:m];
x = mean(a);
avg = mean(x);
I keep getting, in red letters:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in problem4c (line 3)
a = [1:m; 2:m; 3:m; 4:m; 5:m];
Error in run (line 96)
evalin('caller', [script ';']);

Antworten (2)

Image Analyst
Image Analyst am 15 Feb. 2016
The rows don't have all the same number of columns. Instead of putting the vectors into different rows with semicolons, put them into the same row with commas:
a = [1:m, 2:m, 3:m, 4:m, 5:m];
  2 Kommentare
Jonathan Trencheny
Jonathan Trencheny am 15 Feb. 2016
I see, and that makes sense, however what would we do if we need to keep it a matrix?
Walter Roberson
Walter Roberson am 15 Feb. 2016
a = {1:m; 2:m; 3:m; 4:m; 5:m};
x = cellfun(@mean, a);

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 15 Feb. 2016
I don’t know what the problem statement is, but from what I’ve seen, one option might be:
m = 4;
v = m:-1:0;
c = cumsum(0:m);
s = sum(1:m) - c;
avg = s./v
avg =
2.5 3 3.5 4 NaN

Kategorien

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by