Clear Filters
Clear Filters

Loop that creates Variable from other numbered variable

1 view (last 30 days)
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
Walter Roberson
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
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:

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 10 Oct 2023
This can be done realtively easy if this is what you want:
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 ii=1:N
db(ii) = {corrcoef(A,B(ii,:))};
DB_final(ii)=db{1,ii}(1,2);
end
celldisp(db)
db{1} = 1.0000 -0.8204 -0.8204 1.0000 db{2} = 1.0000 -0.8003 -0.8003 1.0000 db{3} = 1.0000 -0.5224 -0.5224 1.0000 db{4} = 1.0000 0.8210 0.8210 1.0000 db{5} = 1.0000 0.3468 0.3468 1.0000
DB_final % This is how you cna get one final value of the diag elements
DB_final = 1×5
-0.8204 -0.8003 -0.5224 0.8210 0.3468

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 9 Oct 2023
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)
db{1} = 1.0000 -0.3243 -0.3243 1.0000 db{2} = 1.0000 0.3257 0.3257 1.0000 db{3} = 1.0000 -0.8269 -0.8269 1.0000 db{4} = 1.0000 0.8554 0.8554 1.0000 db{5} = 1.0000 0.3785 0.3785 1.0000
Simularly, you can apply for toher variables as well.
  1 Comment
Ayoub
Ayoub on 10 Oct 2023
thank you , this was very useful
i have one more question
how can i get the results like this instead:
db{1} =
-0.3243
db{2} =
0.3257
db{3} =
-0.8269
db{4} =
0.8554
db{5} =
0.3785
i only want the number that is not 1

Sign in to comment.

Categories

Find more on Get Started with Wavelet Toolbox 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!