Loop that creates Variable from other numbered variable
Show older comments
Hello
PROBLEM1
i want to create variable db1,db2 ....db10 where
db1 = corrcoef(A,B1);
i already have B1,B2...B10 created
i tried this
for i=10
db(i) = corrcoef(A,B(i));
end
and i got this msg
"Undefined function or variable 'B'."
Problem 2:
i want to keep doing the process with other variable after
sym1,sym2....sym10
then add all the values to a table
so i'm using this long line
Wavelet = ["haar";"fk4";"fk6";"fk8";"fk14";"fk18";"fk22";"db1";"db2";"db3";"db4";"db5";"db6";"db7";"db8";"db9";"db10";"sym1";"sym2";"sym3";"sym4";"sym5";"sym6";"sym7";"sym8";"sym9";"sym10";"coif1";"coif2";"coif3";"coif4";"coif5";"bior1.1";"bior1.3";"bior1.5";"bior2.2";"bior2.4";"bior2.6";"bior2.8";"bior3.1";"bior3.3";"bior3.5";"bior3.7";"bior3.9";"bior4.4";"bior5.5";"bior6.8";"rbior1.1";"rbior1.3";"rbior1.5";"rbior2.2";"rbior2.4";"rbior2.6";"rbior2.8";"rbior3.1";"rbior3.3";"rbior3.5";"rbior3.7";"rbior3.9";"rbior4.4";"rbior5.5";"rbior6.8";"dmey"];
but i want something shorter and faster to write
5 Comments
Dyuman Joshi
on 9 Oct 2023
"i want to create variable db1,db2 ....db10 where"
Dynamically naming variables is not recommended. Read - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
"i already have B1,B2...B10 created"
Again, not a good idea to generate variables like that.
How did you create them? An efficient way to work is be to stack them in an array B and then use indexing.
It will be better if you share the code you are working with.
As regard to problem 2, I am not sure if I understand it.
Ayoub
on 9 Oct 2023
Dyuman Joshi
on 9 Oct 2023
First you say - "i already have B1,B2...B10 created"
Then when I asked, how did you create them, you say - "B is actually a signal with a lenght of 2001"
Similarly, you say - "i want to create variable db1,db2 ....db10"
Then you say - "DB will is [4x4] size"
Your statements do not make any sense.
As I said before, it would be better if you share your code.
Walter Roberson
on 9 Oct 2023
Wavelet = ["haar";"fk4";"fk6";"fk8";"fk14";"fk18";"fk22";"db1";"db2";"db3";"db4";"db5";"db6";"db7";"db8";"db9";"db10";"sym1";"sym2";"sym3";"sym4";"sym5";"sym6";"sym7";"sym8";"sym9";"sym10";"coif1";"coif2";"coif3";"coif4";"coif5";"bior1.1";"bior1.3";"bior1.5";"bior2.2";"bior2.4";"bior2.6";"bior2.8";"bior3.1";"bior3.3";"bior3.5";"bior3.7";"bior3.9";"bior4.4";"bior5.5";"bior6.8";"rbior1.1";"rbior1.3";"rbior1.5";"rbior2.2";"rbior2.4";"rbior2.6";"rbior2.8";"rbior3.1";"rbior3.3";"rbior3.5";"rbior3.7";"rbior3.9";"rbior4.4";"rbior5.5";"rbior6.8";"dmey"];
could be written as
Wavelet = ["haar", "fk"+[4 6 8 14 18 22], "db" + (1:10), "sym"+(1:10). "coif" + (1:5), "bior1."+[1 3 5], "bior2."+(2:2:8), "bior3."+(1:2:9) <etc>].';
Stephen23
on 10 Oct 2023
"B is actually a signal with a lenght of 2001 "
Your original question shows this error message: "Undefined function or variable 'B'." Which tells us that B actually does not exist in that workspace.
Your question presumes a particular solution, but does not explain what the actual problem is:
Accepted Answer
More Answers (1)
A quick note: in your loop code, there is an err that need to be fixed:
% your code:
% for i=10 computes for i=10 only and skips 1 to 9
% db(i) = corrcoef(A,B(i));
% end
Besides, another point is what is the size of variable B1, B2, ... Bn?
If B1, B2, ... Bn are separate variables (with the same size), you can combine them into one variable (one matrix).
Another point is the size of variable db needs to be noted here, and maybe it can be stored in a cell array from every iteration. E.g.:
% Note that size of A and B compatible to compute correlation coeff's:
A=randi(15, 1, 5); % 1 - by - 5
B1=randi(13, 1,5); % 1 - by - 5
B2=randi(13, 1,5); % 1 - by - 5
B3=randi(13, 1,5); % 1 - by - 5
B4=randi(13, 1,5); % 1 - by - 5
B5=randi(13, 1,5); % 1 - by - 5
...
B = [B1; B2;B3;B4;B5];
N = size(B,1);
db = cell(1,N);
for i=1:N
db(i) = {corrcoef(A,B(i,:))};
end
celldisp(db)
Simularly, you can apply for toher variables as well.
1 Comment
Ayoub
on 10 Oct 2023
Categories
Find more on Discrete Multiresolution Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!