How to change the value of a variable each time a loop runs?
Show older comments
Hello, I am working with this code below:
D = cell(num_ds, 1);
for k = 1 : num_ds
D{k} = A{k,1}+DI-20*log(r)-alpha*r+deltaL-10.8;
end
It is the outdoor sound propagation equation. This loop as it stands now produces the variable D that has 5 cells containing five 1x8 matrices representing decibel values. I need to be able to change the value of alpha for each cell, i.e. say the matrix contains the values 62,66,45,78,36,75,36,75 each one of these values needs to be computed with a different value of alpha. I am not sure how to get this to work to not only one matrix but all of them consistently.
I hope I explained this well enough!
The values of alpha that are needed to run through are 0.271, 0.579, 1.2, 3.27, 11, 36.2, 91.5 and 154
Answers (1)
James Tursa
on 11 Oct 2016
Edited: James Tursa
on 11 Oct 2016
Do you want each result in a different cell? E.g.,
alpha = [0.271, 0.579, 1.2, 3.27, 11, 36.2, 91.5, 154];
D = cell(num_ds, numel(alpha));
for k = 1 : num_ds
for m = 1 : numel(alpha)
D{k,m} = A{k,1}+DI-20*log(r)-alpha(m)*r+deltaL-10.8; % fixed typo
end
end
6 Comments
Alison Alexsy
on 11 Oct 2016
James Tursa
on 11 Oct 2016
Oops. Typo. Use alpha(m) instead of alpha{m}.
Alison Alexsy
on 11 Oct 2016
James Tursa
on 11 Oct 2016
Edited: James Tursa
on 11 Oct 2016
What is in the A{k,1}? A scalar, vector, or matrix? E.g., does each A{k,1} contain a 1x8 vector? What are the sizes of the other variables? If r is a scalar, it is not clear to me why your original code wouldn't work for you. If r is a vector, them maybe this does what you want:
D{k} = A{k,1}+DI-20*log(r)-alpha.*r+deltaL-10.8;
Alison Alexsy
on 11 Oct 2016
Alison Alexsy
on 11 Oct 2016
Edited: Alison Alexsy
on 11 Oct 2016
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!