How to create a row vector from rows with different length in a loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Lacki
am 12 Apr. 2017
Bearbeitet: Stephen23
am 13 Apr. 2017
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 Kommentar
Stephen23
am 12 Apr. 2017
@Lacki: By the way, all of the vectors that you show are column vectors, not row vectors.
Akzeptierte Antwort
Stephen23
am 12 Apr. 2017
Bearbeitet: Stephen23
am 13 Apr. 2017
Comma separated lists are your friend. Learn to use them:
>> C = {[1;2;3],[1;3;4;16;],[2;3;6;16;25]};
>> vertcat(C{:})
ans =
1
2
3
1
3
4
16
2
3
6
16
25
"after asking google and youtube for some hours" It is good that you tried searching for information, but honestly I would not trust anything found on youtube. MATLAB has the best, easiest to read documentation of any language I have ever used: it should be your first, second, and third sources of information. The internet contains a lot of bad advice on MATLAB, even on stack exchange, usually written by people who do not understand code vectorization or how to make JIT-compiled code efficient.
So while youtube might be great for watching kittens or whatever it is that young people do these days for "hobbies", there is nothing to replace the MATLAB documentation itself (you know, documentation is that thing that tells you how something works). Use it. Practice using it. Use it more.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!