How to concatenate horizontally a cell into another cell based on a vector
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to concatenate horizzontally with a criteria based on a vector this cell into another cell. All the double matrices inside the "wl_tmp" cell have the same number of rows, but not the same number of colums!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/581741/image.png)
I have this vector and in my idea, the elements with the same value should be grouped together (concatenating them horizontally from left to right).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/581746/image.png)
For example: the final cell is "wl_final":
wl_final{1} = [wl_tmp{1}, wl_tmp{2}, wl_tmp{3}, wl_tmp{4}, wl_tmp{5}, wl_tmp{6}, wl_tmp{7}, wl_tmp{8}, wl_tmp{9}, wl_tmp{10}, wl_tmp{11}];
wl_final{2} = [wl_tmp{12}, wl_tmp{13}];
%and so on...
I found a solution with a for loop but because of the high amount of data, it's terribly slow and I wanted to figure out how I could use "accumarray" instead, but I cannot understand how that function works exactly and how to use it for my purposes.
wl_tot = length(wl_idx);
wl_final = cell(1,length(pX_i_ref_wl)); %"pX_i_ref_wl" contains just the name of the groups: in this case is a 1x11 string
clear tmp;
for k=1:wl_tot
if wl_idx(k)~=wl_idx(max(k-1,1)) || k==1
tmp = wl_tmp{k};
else
tmp = [tmp, wl_tmp{k}]; %#ok<AGROW>
end
if k==wl_tot || wl_idx(k+1)~=wl_idx(k)
wl_final{wl_idx(k)} = tmp;
clear tmp;
end
end
P.S. I cannot group them before, because the "wl_tmp" comes from a parfor loop with sliced variables and I'd prefer to group them later!
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!