Matrix calculation, sum of two rows in each column
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
monmatlab
le 20 Déc 2016
Commenté : Andrei Bobrov
le 20 Déc 2016
I have a matrix d and I want to calculate for every column the sum of each to numbers. The Result should be the result matrix
d= 2 3 7
6 7 8
9 1 5
4 3 5
6 7 8
2 3 6
result= 8 10 15
13 4 10
8 10 14
result=[];
[x,y]=size(d);
for y=1:length(d(:,y))
for x=1:2:length(d(x,:))
[x,y]=size(d);
res=d(x)+d(x+1);
result=[result res];
end
end
I just get a vector consisting of 5. What should I change in the code? Thank you!
1 commentaire
Jos (10584)
le 20 Déc 2016
I suggest you debug your code, tracing each step. the variables x and y may not behave as you expect.
Réponse acceptée
Andrei Bobrov
le 20 Déc 2016
d= [ 2 3 7
6 7 8
9 1 5
4 3 5
6 7 8
2 3 6];
result = squeeze(sum(reshape(d',size(d,2),2,[]),2))';
4 commentaires
Andrei Bobrov
le 20 Déc 2016
for 2016b
result = squeeze(sum(reshape(d',size(d,2),2,[]).*[1,3],2))'
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!