How to create a row vector from rows with different length in a loop?
Show older comments
Hi everyone,
I am pretty new to Matlab, so my question might be trivial but after asking google and youtube for some hours I finally have given up and ask myself.
I have a loop importing my data sets (which can vary in numbers) to a struct. From the struct, I have a loop shaving everything I don't need for my current analysis resulting in a cell array with one line of cells with each cell containing a 2-column matrix. Each pair has a different lenght and I cannot make a guess how long they will be and that is where my problems start... All I want to do is take the values from colum 1 from each data set and build a new row with each values added below each other (e.g. [1;2;3], [1;3;4;16;], [2;3;6;16;25] into [1;2;3;1;3;4;16;2;3;6;16;25]. I THINK I might solve the problem by vastly overerstimating the possible size (lets say fill 100.000 rows with zeroes) and cut off everything I don't need afterwards but I believe theres a much more elegant solution to it.
When I import all data sets by hand, I can easily write something like
row_consensus = [data1(:,1);data2(:,1);data3(:,1)];
resulting in a long row, which I can order and use another loop to sort my y-data with after creating a matrix filled with 0s as I now know how many rows I will need.
When I try to transfer the first line of code to any kind of loop, I get the "Subscripted assignment dimension mismatch", which makes sense in some way as my dimensions are indeed off (as all data has different length). As I said, I don't know how large the data sets are (some are onyl 20, some are 1500 data pairs), so perallocating a matrix and filling it with zeroes does not work at this step. I tried to work directly from the struct or the cell array, but nothing I try seems to work. The code I am currently tryingis something like (k is the number of data sets imported so the loop works for any amount of data sets)
consensus = [all_data{1,1}(:,1)];
for m = 1:k
consensus(end+1,1)= all_data{1,m}(:,1);
end
Thanks a lot in advance!
1 Comment
Stephen23
on 12 Apr 2017
@Lacki: By the way, all of the vectors that you show are column vectors, not row vectors.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!