How to concatenate vectors indexed with a for loop?
18 次查看(过去 30 天)
显示 更早的评论
Hello,
I want to create a matrix with the cocatenated product of all column vectors I extracted from my original dataset. I need to select three row vectors in each loop iteration and concatenate these vectors to form a final column matrix. So far, my for loop gave me a matrix with the last iteration only. I need to make multiple iterations of this trialNum indexing. However, I only showed 2 here.
Here's a sample of my code that depicts what I'm trying to do. Feel free to point out how I could transpose these vectors more ellegantly (in the last part).
Thank you so much in advance.
modes = 2:4;
i = 3;
j = 7;
trialNum = 1;
data_i_1 = [save_sx1_hat{i,trialNum}(modes,1:500)];
data_j_1 = [save_sx1_hat{j,trialNum}(modes,1:500)];
trialNum = 2;
data_i_2 = [save_sx1_hat{i,trialNum}(modes,1:500)];
data_j_2 = [save_sx1_hat{j,trialNum}(modes,1:500)];
Alldata_i = [data_i_1 data_i_2]';
Alldata_j = [data_j_1 data_j_2]';
All_Data = [Alldata_i; Alldata_j]
All_Labels = [zeros(1000,1)+1; zeros(1000,1)+2];
0 个评论
回答(1 个)
Mrinal Anand
2023-7-8
As per my understanding you want your above code to run in a loop with "trialNum" ranging from 1 to the desired number of iterations. You can do the following modifications to your code:
modes = 2:4;
i = 3;
j = 7;
numIterations = 3; % Number of iterations
All_Data = [];
All_Labels = [];
for trialNum = 1:numIterations
data_i = [save_sx1_hat{i, trialNum}(modes, 1:500)];
data_j = [save_sx1_hat{j, trialNum}(modes, 1:500)];
Alldata_i = data_i';
Alldata_j = data_j';
All_Data = [All_Data; Alldata_i; Alldata_j];
All_Labels = [All_Labels; zeros(size(Alldata_i, 1), 1) + 1; zeros(size(Alldata_j, 1), 1) + 2];
end
Here, the "numIterations" variable takes the number of trials you want to run and then encloses your concatenation logic in a for loop for the same number of trials. I am not sure there is any other way to transpose the vector more efficiently than how it is done here.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!