cell to double with mismatched dimensions
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jonathan Soucy
am 29 Aug. 2016
Kommentiert: Jonathan Soucy
am 29 Aug. 2016
Input:
a = cell(3,1);
a{1,1} = [1 2 3];
a{2,1} = [1 2 3 4];
a{3,1} = [1 2 3 4 5];
Desired output:
b = [[1 2 3 0 0];[1 2 3 4 0];[1 2 3 4 5]];
What would be the best way to get this output from this input?
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 29 Aug. 2016
Bearbeitet: Geoff Hayes
am 29 Aug. 2016
Jonathan - one way would be to use cellfun to apply a function to each element of your cell array padding each with zeros so that all arrays are of the same length. For example, we can get the maximum length as
maxLength = max(cellfun(@(x)length(x),a));
We would then use cellfun again to pad the arrays with zeros using repmat (which just repeats a matrix) as
aPadded = cellfun(@(x)[x repmat(0,1, maxLength - length(x))],a,'UniformOutput',false);
b = cell2mat(aPadded);
Try the above and see what happens!
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!