Loop that creates Variable from other numbered variable

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

"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.
B is actually a signal with a lenght of 2001 Same goes for A DB will is [4x4] size. I used db1,db2..db10 names because those are names of wavelets and it's easy for me to know which one from which
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.
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>].';
"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:

댓글을 달려면 로그인하십시오.

 채택된 답변

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

추가 답변 (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)
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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Discrete Multiresolution Analysis에 대해 자세히 알아보기

질문:

2023년 10월 9일

답변:

2023년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by