please help simplify this for-loop-hell
Show older comments
The code below works as it should, but its slooow. I have a data variable with x number of cells. I want every row in column 6 of each cell, to match up and leave the respectively row value from column 3, in those rows. If the value from column 6 not match between the cells in data, I instead place a zero. I describe the data first:
data = 5; % data: cells with different size of rows e.g. 2000x6, 4000x6, 8000x6.
biggest_data_size = 8000; % can be other size as well (the one cell in data with most rows)
biggest_data_series = % this is 8000x1 series the 6. column in data{biggest_data_idx}. Unique increasing number (the one cell in data with most rows). I want this one as the first column in the result.
data_qty = 5; % can be other size as well, its the number of cells in data
result= [ biggest_data_series zeros(biggest_data_size,data_qty) ];
for i = 1 : data_qty
data_current_size = size(data{i},1);
for j = 1 : biggest_data_size
for h = 1 : data_current_size
if result(j,1) == data{i}(h,6)
result(j,i+1) = data{i}(h,3);
end
end
end
end
Hope I am clear enough. Any suggestions is very appreciated...
4 Comments
Show a small sample dataset(*) and what you want the result to be...it's much easier to attack the problem itself with an illustration rather than try to figure out a convoluted piece of code.
(*) Arrays of 5 and 7 or 10 elements serve to illustrate just as well as does 2000 and are small enough to visualize and post.
Stephen23
on 1 Nov 2018
@Martin: your example is not clear. You wrote in your question that you want the first column of result to be from "...(the one cell in data with most rows)", but in your example data{2} has the most rows yet you used data{1} for the first column. Please explain how this works.
Martin
on 1 Nov 2018
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!