フィルターのクリア

How to distribute each column of a cell array into a individual cells of another array without for loops?

6 ビュー (過去 30 日間)
Dear Matlab users,
I am looking for a way without using for loops and deal (if possible) to distribute each column of a cell array containing characters into individual separate nested cells of another cell array. This should give me a final cell array with two levels, each representing a different identifier of the data (the top level = the set, the second level has a different cell for each data input).
I have managed to do this using for loops but wanted to know of if anyone has/can think of a neater/more efficient way of doing this or is this already the most appropriate way of approaching this problem?
Instead to improve this do I need to use different data structures (structures or tables) and change the approach completely?
Below is a minimal working example of my code which achieves what I want.
Hoping to improve my matlab skills :)
Thanks,
Samuele
% Inputs
numOperators = 5;
commonSets = {'set1';'set2';'set3'};
% Each column corrisponds to a common set, ie label 1, 2, 3 in column 1
% belong to set 1
commonSetLabels ={'Label_1','Label_1','Label_1';...
'Label_2','Label_2','Label_2';...
'Label_3','Label_3','Label_3'};
% Following the same logic there is an array containing the x coordinates
% of each label
xCoords = [1,1,1;
2,2,2;
3,3,3];
numLabels = size(commonSetLabels,1);
% Preallocation
interoperatorMarkers = cell(size(commonSets,1),1);
% Loop through each operator
for ii = 1:numOperators
% -------------------------------------- %
% Is it possible to achieve this without looping
% Loop through each common set
for jj=1:size(commonSets,1)
[interoperatorMarkers{jj,1}{:,1}{1:numLabels,ii}] = deal(commonSetLabels{:,jj});
interoperatorMarkers{jj,1}{:,2}(1:numLabels,ii) = xCoords(:,jj);
end
% -------------------------------------- %
end
% Top level representing each common set
interoperatorMarkers
interoperatorMarkers = 3×1 cell array
{1×2 cell} {1×2 cell} {1×2 cell}
% Second level containing the different data, ie the labels in one and the
% x coordinates in the other
interoperatorMarkers{1,1}
ans = 1×2 cell array
{3×5 cell} {3×5 double}
  3 件のコメント
dpb
dpb 2022 年 9 月 8 日
Agree with @Stephen23 completely -- I'd say your Q? of "do I need to use different data structures (structures or tables) and change the approach completely?" is the right question to ask and that the answer is definitely "YES!"
It's not clear just what your overall data content is without more digging into the explanation to try to figure out just what is really intended here, but I'd venture a table could hold it all quite nicely; you just create a grouping variable for each identifier needed instead where you're building nested levels.
Samuele Gould
Samuele Gould 2022 年 9 月 8 日
Thank you both for the answers. I think the most straight forward method given where the data is coming from and what I need to do with it later (which would take a lengthier explanation) is to simplify the design of the cells into ones with more dimension. In the future I definitly think more about how I can use tables in this sort of scenario thanks for the suggestion.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by