Effacer les filtres
Effacer les filtres

How can I write a for loop to do algebra over multiple arrays?

1 vue (au cours des 30 derniers jours)
Havi Tripathi
Havi Tripathi le 20 Août 2021
Commenté : Havi Tripathi le 20 Août 2021
I have 4 1x28 arrays.
I want to solve an equation for time, also linearly spaced as 1x28, for each value in this row.
a = linspace(3,28,28);
b = linspace(4,28,28);
c = linspace(5,28,28);
d = linspace(6,28,28);
time = linspace(1,28,28);
for i = 1:28:
value = a.*time ./ (c+d)
end
So, in the end, value should be a 28x28 array right. The first column of value, for example, should be value evaluated at time 1.
As it goes downward in column 1, it should be evaluated at time 2, etc till 28.
I'm not sure how to actually implement this. When I am doing it regularly, I get back a 1x28 array from value.
Does anyone know the approach or solution I am looking for?

Réponse acceptée

Wan Ji
Wan Ji le 20 Août 2021
Do loop with indices of t:
a = linspace(3,28,28)';
b = linspace(4,28,28)';
c = linspace(5,28,28)';
d = linspace(6,28,28)';
time = linspace(1,28,28);
value = zeros(28,28);
for i = 1:28
value(:,i) = a.*time(i)./ (c+d);
end
And then the value becomes 28*28 as you want.
  1 commentaire
Havi Tripathi
Havi Tripathi le 20 Août 2021
Thanks for the quick help! I understand indexing better now.

Connectez-vous pour commenter.

Plus de réponses (1)

Simon Chan
Simon Chan le 20 Août 2021
Try this:
A = arrayfun(@(x) x.*time./(c+d), a, 'UniformOutput', false)
result = cell2mat(A')

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by