Error Using Accumarray, Third input SZ must be a full row vector with one element for each column of SUBS

1 Ansicht (letzte 30 Tage)
Hi All, I am having trouble using accumarray. I am getting the following error and I am having trouble figuring out why. Thanks in advance.
Error using accumarray Third input SZ must be a full row vector with one element for each column of SUBS.
Error in approach2 (line 13) p = accumarray(subs, val, sz);
And the following is the code:
d = [3 3 4 4 4 5 6 5 5 4 3 2 3 4 3 2 5 6 6 5 5 4 3];
precision = 1;
f = (round(d/precision)).*precision;
[c, ~, ic] = unique(f);
N = numel(c);
subs = [ic(1:end - 3), ic(2:end-2), ic(3:end-1), ic(4:end)]; val = 1; sz = [N^2, N^2];
p = accumarray(subs, val, sz);
p = bsxfun(@rdivide, p, sum(p, 2));

Antworten (2)

Star Strider
Star Strider am 27 Jul. 2015
If you don’t want to use the third argument, you don’t need to include it.
See if substituting this for your accumarray call does what you want:
p = accumarray(subs, val);
  2 Kommentare
Ellie
Ellie am 27 Jul. 2015
Well, i think I found the problem by doing that. p becomes a 4-D array, which is not what I want. Any idea how to fix that? or is that a new question?

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 27 Jul. 2015
Bearbeitet: Walter Roberson am 27 Jul. 2015
When you use accumarray, each row of subs is used as an index into array, as if
idx = num2cell(subs(K,:));
result(idx{:}) = val(K);
Your subs is 4 wide, so you are asking to create a 4 dimensional array.
My guess is that you want sz = [N, N, N, N]
You could always reshape() the result to N^2 x N^2
  1 Kommentar
Walter Roberson
Walter Roberson am 28 Jul. 2015
subs_sq = [(subs(:,2) - 1)*N + subs(:,1), (subs(:,4) - 1)*N + subs(:,3)];
p = accumarray(subs_sq, val);
p = bsxfun(@rdivide, p, max(1,sum(p, 2)));
Or alternately,
p = reshape(accumarray(subs, val, [N, N, N, N]), [N^2, N^2]);
p = bsxfun(@rdivide, p, max(1,sum(p, 2)));
Yes this does create a temporary 4-dimensional array, and then immediately reshapes it to 2D. It also has exactly the same effect as the subs_sq version that builds the 2D array directly by assigning a number to each possible 2D vector.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Multidimensional Arrays 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