How to distribute each column of a cell array into a individual cells of another array without for loops?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
% Second level containing the different data, ie the labels in one and the
% x coordinates in the other
interoperatorMarkers{1,1}
3 Kommentare
dpb
am 8 Sep. 2022
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.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion 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!