Create an array from a cell array
6 ビュー (過去 30 日間)
古いコメントを表示
Given the following cell array
G= {[1 2 1 2 3 4 5 4 5 4 6 3 9 3 9;
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75],
[1 1 1 1 2 3 4 4 4 5 4 6 3 6 3 9;
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80]}
and consider just G {1,1} (then I will need to iterate to consider also G{1,2})
I want to obtain theese arrays
V1=[1 1 ; 5 0];
V2=[2 2; 10 0];
V3=[3 3 3; 25 0 0];
V4=[4 4 4; 30 0 0]
V5=[5 5, 35 0]
V6=[6; 55]
V7=[9 9, 65 0]
Visualizing G{1,1}
I want to obtain 7 arrays, because 7 is the number of different elements that are inside the first raw of G{1,1}: (1 2 3 4 5 6 9). If the number of diversities is more x, I would like to obtain x arrays.
Then each vector has, in the first raw, the elements repeated as many times as they are inside the first raw of G{1,1}. Instead, in the second raw of each arrays we want to have as many zeros as the columns above, but in the first position (first column, second raw) we want to report the value that is present in the second raw of G{1,1} when the element appear for the first time.
For example, the element 9 appear the first time with value 65, so in V9 we will have 65, as indicated by yellow circles in the figure above.
May someone help me?
0 件のコメント
採用された回答
Guillaume
2019 年 9 月 24 日
編集済み: Guillaume
2019 年 9 月 24 日
For just one element of the cell array, eg. G{1}:
[uval, loc1, ids] = unique(G{1}(1, :)); %get unique values of first row, with location where first occurence is found and unique id for each values
count = accumarray(1, ids)'; %get histogram (count) of each unique value
result = arrayfun(@(v, s, n) [repelem(v, n); s, zeros(1, n-1), uval, G{1}(2, loc1)], count, 'UniformOutput', false);
celldisp(result)
To apply to each element of G just wrap it in a loop:
result = cell(size(G));
for gidx = 1:numel(G)
[uval, loc1, ids] = unique(G{gidx}(1, :));
count = accumarray(ids, 1)';
result{gidx} = arrayfun(@(v, s, n) [repelem(v, n); s, zeros(1, n-1)], uval, G{idx}(2, loc1), count, 'UniformOutput', false);
end
5 件のコメント
その他の回答 (2 件)
Adam Danz
2019 年 9 月 24 日
編集済み: Adam Danz
2019 年 9 月 24 日
This solution loops through each element of cell array "G" and produces an output "V" which is a cell array the same size as G. See the notes the follow the code to understand how to retrieve your "V1', "V2", etc variables.
G= {[1 2 1 2 3 4 5 4 5 4 6 3 9 3 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 ],[1 1 1 1 2 3 4 4 4 5 4 6 3 6 3 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 ]}
% Loop through each element of cell array G
V = cell(size(G));
for i = 1:numel(G)
unqVals = unique(G{i}(1,:)); % vector of unique values in 1st row of G{i}
nReps = sum(unqVals==G{i}(1,:).',1); % number of repetitions of each unique value (requires r2016b or later)
row2val = G{i}(2,arrayfun(@(x)find(G{i}(1,:)==x,1,'first'),unqVals));
V{i} = arrayfun(@(x,r,v)[x*ones(1,r);[v,zeros(1,r-1)]],unqVals,nReps,row2val,'UniformOutput',false)
end
"V1" for the first and second elements of G are stored in V{1}{1} and V{2}{1}
"V2" for the first and second elements of G are stored in V{1}{2} and V{2}{2}
"Vn" for the first and second elements of G are stored in V{1}{n} and V{2}{n}
the cyclist
2019 年 9 月 24 日
Another possibility:
[i,j,k] = unique(G{1}(1,:));
numberUniqueG = numel(i);
for ng = 1:numberUniqueG
ngCount = sum(k==ng);
V{ng} = zeros(2,ngCount);
V{ng}(1,:) = ng;
V{ng}(2,1) = G{1}(2,j(ng));
end
Wrap this in a loop over the elements of G.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!