Reference multiple elements in a cell array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrew Bellocchio
am 30 Nov. 2017
Beantwortet: Stephen23
am 1 Dez. 2017
Hi all, I am unsure on the best way to reference multiple elements in a cell array. Suppose I have 4 parts and each is a cell. All parts are contained within a larger cell array as:
Part01={'Exponential' 1/lambda_A 0 age1 0};
Part02={'Exponential' 1/lambda_B 0 age2 0};
Part03={'Exponential' 1/lambda_C 0 age3 0};
Part04={'Exponential' 1/lambda_D 0 age4 0};
Parts={Part01;Part02;Part03;Part04};
Each age# value is a single number. I want to create a single vector like this:
AgeVector=[age1, age2, age3, age4]
I can do so using:
PartAgeVector=[Parts{1}{4} Parts{2}{4} Parts{3}{4} Parts{4}{4}];
This seems awfully ugly and inefficient. The number of Parts will change with each use and I want to avoid manually coding like the last line of code. How may I most efficiently create a single vector.
Thank you! A.Bellocchio
2 Kommentare
Stephen23
am 1 Dez. 2017
"This seems awfully ugly and inefficient"
It is. If you simply put your numeric data into a numeric array then this entire problem goes away.
Akzeptierte Antwort
KSSV
am 1 Dez. 2017
PartAgeVector = cell2mat(cellfun(@(x) x(1,4), Parts))
1 Kommentar
Andrew Bellocchio
am 1 Dez. 2017
Bearbeitet: Andrew Bellocchio
am 1 Dez. 2017
Weitere Antworten (1)
Stephen23
am 1 Dez. 2017
>> C = {1,11,111;2,22,222;3,33,333;4,44,444}
1 11 111
2 22 222
3 33 333
4 44 444
>> [C{:,3}]
ans =
111 222 333 444
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!