Concatenate and extract elements of cell array to a non-cell array
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Morgan Clendennin
am 6 Dez. 2016
Bearbeitet: Stephen23
am 6 Dez. 2016
Hi,
So I am trying to take a cell array and extract the elements of it, concatenate them, and place them in the corresponding locations in a non-cell array. I'm sure that doesn't make much sense so let's say I have the follow cell array
A = {[1 3],[],[2 3 7 8];[2 4 5 7],[4 7 8],[];[],[],[]}
Now I want to take this cell array take the first element [1 3], extract the elements and concatenate them and place them as the first element of a non-cell array. This will continue for the rest of the elements of the cell array. If a cell has no elements, the corresponding location in the new array would equal zero. The final non-cell array would look like following
B = [13,0,2378;2457,478;0,0,0]
0 Kommentare
Akzeptierte Antwort
KSSV
am 6 Dez. 2016
A = {[1 3],[],[2 3 7 8];[2 4 5 7],[4 7 8],[];[],[],[]} ;
A = A(:) ;
B = zeros(size(A)) ;
for i = 1:numel(A)
if ~isempty(A{i})
B(i) = str2num(strrep(num2str(A{i}), ' ', '')) ;
end
end
B = reshape(B,3,3)
Can be achieved without loop also...but understanding is more important then code.
0 Kommentare
Weitere Antworten (1)
Stephen23
am 6 Dez. 2016
Bearbeitet: Stephen23
am 6 Dez. 2016
A simple solution:
>> A = {[1 3],[],[2,3,7,8];[2,4,5,7],[4,7,8],[];[],[],[]};
>> B = [13,0,2378;2457,478,0;0,0,0]
ࣳB =
13 0 2378
2457 478 0
0 0 0
>> C = cellfun(@(c)sum([0,c].*10.^(numel(c):-1:0)),A)
C =
13 0 2378
2457 478 0
0 0 0
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices 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!