MATLAB Answers

Splitapply on a matrix of 1X1000

2 views (last 30 days)
JFz
JFz on 5 Mar 2018
Commented: JFz on 5 Mar 2018
I have a matrix of 1x1000 numbers and my G is 1. When I do the splitapply on it: res = splitapply(@sum, mat, G);
The res Is the sum of the 1000 numbers. What I want is just the one row returned, i.e., res = mat;
How to get that? This is a special case. In general, there are many rows.
Thanks.

  0 Comments

Sign in to comment.

Accepted Answer

Rik
Rik on 5 Mar 2018
res = splitapply(@(x) sum(x,1), mat, G);
This is about 10 times slower for mat=rand(1,1000), but it forces the direction of summation. You could also include a conditional statement:
if size(mat,1)==1
res=mat;
else
res = splitapply(@sum, mat, G);
end

  1 Comment

JFz
JFz on 5 Mar 2018
Thanks! I will use the if statement.

Sign in to comment.

More Answers (0)

Tags

Products


Translated by