MATLAB Answers

managing two variables in sum function

40 ビュー (過去 30 日間)
Fred
Fred 2013 年 2 月 10 日
回答済み: Steven Lord 2020 年 2 月 6 日
Hi, I want to manage two variables in a sum function in a way that sum(i*cos(ik)); which i=1:N , k=1:M but I want only want the SUM with respect to i and create a vector which has M values in other way i should be sumed but k only takes a value each time. what can I do? If I should use loops how can I do that? Thanks in advance

  0 件のコメント

Sign in to comment.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 10 日
N=10;
M=20;
ii=1:N
for k=1:M
som(k)=sum(ii.*cos(ii*k));
end

  1 件のコメント

Raj Patel
Raj Patel 2020 年 2 月 6 日
No

Sign in to comment.

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 2 月 6 日
Use element-wise operations to create a matrix of data to sum then call sum with a dimension input.
[c, r] = meshgrid(1:5, 1:6);
M = c.^r
sum(M, 1) % Sum down columns
sum(M, 2) % Sum along rows

  0 件のコメント

Sign in to comment.

タグ

製品


Translated by