How to change the value of a variable each time a loop runs?

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)

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

Cell contents reference from a non-cell array object.
Error in Sound_Code (line 60) D{k,m} = A{k,1}+DI-20*log(r)-alpha{m}*r+deltaL-10.8;
Oops. Typo. Use alpha(m) instead of alpha{m}.
Fixed it, it runs now however it produces a 5x8 matrix, is there anyway it can have the same format of the other variables I have where D has five different cells containing each individual matrix?
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;
A{1,1} - A{5,1} are 1x8 vectors
Everything is a scalar except A and now alpha, using that code I got the error:
Error using - Matrix dimensions must agree.
Error in Sound_Code (line 60) D{k} = A{k,1}+DI-20*log(r)-alpha.*r+deltaL-10.8;
I changed alpha to
alpha = [0.271; 0.579; 1.2; 3.27; 11; 36.2; 91.5; 154];
and that fixed it, but now D has blank squares

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 11 Oct 2016

Edited:

on 11 Oct 2016

Community Treasure Hunt

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

Start Hunting!