How to concatenate horizontally a cell into another cell based on a vector
조회 수: 3 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!