MATLAB Answers

create a function for a cell array

1 ビュー (過去 30 日間)
shobhit mehrotra
shobhit mehrotra 2015 年 2 月 15 日
回答済み: Geoff Hayes 2015 年 2 月 15 日
I have a cell array {data} of indices numbers.
C = {1:23, 24:55, 56:102, 103:255, 256:351} (351 data points)
I have a vector of concentrations of a gas CO2 = 1x351 vector
I want to crate a cell array with the CO2 values evaluated at the indices
CO2E = {CO2(1:23), CO2(24:55), CO2(56:102), CO2(103:255), CO2(256:351)}
I have this so far
for mm = 1:length(C)
dataco2(mm) = (co2(dataind{:,mm}))
The end goal is to create a cell array, dataco2 that is the exact same dimension as {C}

回答 (1 件)

Geoff Hayes
Geoff Hayes 2015 年 2 月 15 日
shobhit - you don't say what is wrong with your above code (please do in the future) but if you wish to continue using the loop as above then you should define your CO2E array as a cell array and update it as
CO2E = {};
for mm = 1:length(C)
CO2E{mm} = CO2(C{mm});
It is very similar to what you had, though from your above code, it is unclear why you are using dataind instead of C, and co2 instead of CO2.
An alternative to the above is to use arrayfun which we discussed in another post.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by