Filter löschen
Filter löschen

How can I complete summation of variable named matrices in loop?

1 Ansicht (letzte 30 Tage)
If matrices are variable (1:N) with names s_1:s_N how can I complete summation of the matrices. Below is my current script which errors out for matrix dimensions do not agree. I understand this error and I understand I cannot call matrices using ['s_', num2str(i)] but still not sure how to move forward with summation.
%% s_1, s_2, s_3, s_4 are all known matrices with values 3500x4 double. Matrices will vary - created 4 just for debugging.
N = 4
sumCST1 = zeros(size(s_1));
for i = 2:N
sumCST1 = sumCST1 + ['s_', num2str(i)]
end
Error using + Matrix dimensions must agree.

Akzeptierte Antwort

Branden Chamness
Branden Chamness am 27 Mär. 2017
figured it out from another thread:
sumCST1 = zeros(size(s_1));
for i = 1:N
s = ['s_' num2str(i)];
sumCST1 = sumCST1 + eval(s)
end

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics 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