Create vector of cell arrays of ranges from 0 to the value in each index of another vector

10 views (last 30 days)
I need help writing code that does the following:
Iterate through a vector; for each index, create a cell array that ranges from 0 to that value at that index, in increments of 1. This should create a vector of cell arrays, with each cell being a vector containing the range for each cell of the original vector.
For instance, if the first cell in the original vector is a 6, then the first cell of the cell array should be [0 1 2 3 4 5 6]. Then if the next cell in the original vector is 9, the second cell in the cell array should be [0 1 2 3 4 5 6 7 8 9]. And so on.
Here is my attempt to do this:
max_fr = max(firing_rates); % creates a 1x143 vector of single values
m = [];
m_vector = {};
for i = 1:143
m = max_fr;
m_vector(1:i) = {0:1:m};
end
But this only creates a 1x143 vector of cell arrays that are all the same as the first cell, though it correctly creates [0 1 2 3 4 5 6] for that cell.

Accepted Answer

Akira Agata
Akira Agata on 15 Mar 2020
Like this?
% Example of original cell
oriCell = {6;9;3;8;2};
% Generated cell array
outCell = cellfun(@(x) 0:x,oriCell,'UniformOutput',false);

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!