62 views (last 30 days)
Show older comments
Brooke Yeager on 14 May 2021
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array
Commented: Brooke Yeager on 14 May 2021
Accepted Answer: the cyclist
I know this sounds simple... maybe it's Friday and my brain is fried, so bare with me.
I have a cell array that is 100x8. I simply want the first column of this array to be numbers 1-100, such that the first row of the first column has a 1, the second row of the first column has a 2, the third row of the first column has a 3, and so on until the 100th row of the first column has 100. These numbers will correspond to the trial numbers.
I don't know why I can't figure this out.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
the cyclist on 14 May 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array#comment_1521018
Don't feel bad. Even after decades of use of MATLAB, I sometmes have to tinker around to remember the exact syntax for different operations using cell arrays. (Even with the solution I proposed, I feel in the back of my mind that there is a simpler way.)
Sign in to comment.
Sign in to answer this question.
Accepted Answer
the cyclist on 14 May 2021
Edited: the cyclist on 14 May 2021
Open in MATLAB Online
Here is one way:
N = 100;
% Set up an empty cell array
C = cell(N,8);
% Fill the first column
C(:,1) = num2cell(1:N)
C = 100×8 cell array
{[ 1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 2]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 3]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 4]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 7]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 8]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 9]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[10]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[11]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[12]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[13]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[14]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[15]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[16]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
DGM on 14 May 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array#comment_1521023
There we go. I feel like I'm in the same boat as OP today.
the cyclist on 14 May 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array#comment_1521038
There is always more room in the "Here is one way but I think there is a better way" boat. I am regularly humbled here by the elegant solutions that are obvious (in retrospect).
Brooke Yeager on 14 May 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array#comment_1521078
This worked beautifully. Thank you for your help!!!
Sign in to comment.
More Answers (1)
DGM on 14 May 2021
Open in MATLAB Online
Someone can surely improve on this, but this is one way
C = cell(10,4) % i'm just going to use a smaller example array
C(:,1) = mat2cell((1:10).',ones(10,1),1)
gives
C =
10×4 cell array
{[ 1]} {0×0 double} {0×0 double} {0×0 double}
{[ 2]} {0×0 double} {0×0 double} {0×0 double}
{[ 3]} {0×0 double} {0×0 double} {0×0 double}
{[ 4]} {0×0 double} {0×0 double} {0×0 double}
{[ 5]} {0×0 double} {0×0 double} {0×0 double}
{[ 6]} {0×0 double} {0×0 double} {0×0 double}
{[ 7]} {0×0 double} {0×0 double} {0×0 double}
{[ 8]} {0×0 double} {0×0 double} {0×0 double}
{[ 9]} {0×0 double} {0×0 double} {0×0 double}
{[10]} {0×0 double} {0×0 double} {0×0 double}
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Brooke Yeager on 14 May 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/830228-how-can-i-insert-numbers-1-100-into-the-first-column-of-a-cell-array#comment_1521083
Thank you for your suggestion!!!
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABProgrammingFiles and FoldersFile Operations
Find more on File Operations in Help Center and File Exchange
Tags
- cell arrays
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office