Assign value zero to n elements of cell array

121 次查看(过去 30 天)
Hi all,
I am declaring a cell array to hold some numeric and non numeric data. I want to set the first row, all columns, equal to the numeric value zero. Something like this:
myCell = cell(5, 500);
myCell{1,:} = 0;
Which gives the error:
The right hand side of this assignment has too few values to satisfy the left hand side.
I've tried a number of other things, none worked, so have resorted to doing it in a loop. It works, but I really don't like it. Is there a simple way I'm missing?
Cheers, Karl

采纳的回答

Sean de Wolski
Sean de Wolski 2014-3-17
myCell(1,:) = {0}
  • Left hand side: use () to keep it as cells not braces for extraction of the content of cells.
  • Right handside: make 0 a 1x1 cell so that it can be stuck into all of the cells on the left via scalar expansion
  4 个评论
Flavia Aluisi
Flavia Aluisi 2019-11-19
This is valid also in the case where I have a cell with 10 columns and 71842 rows and I want to fill with the number one only the rows between 2695 and 2746 of column one?

请先登录,再进行评论。

更多回答(2 个)

Parna Bhattacharya
Parna Bhattacharya 2018-3-17
Thanks;
  1 个评论
Benjamin
Benjamin 2019-12-12
This also works:
A =
1 2 3
4 5 6
7 8 9
in order to zero the ith row: (i = 1)
A(1,:) = [0]
>> A =
0 0 0
4 5 6
7 8 9

请先登录,再进行评论。


Jeffery Devereux
Jeffery Devereux 2018-3-7
Nice! Thanks (Had the same issue) Never thought about using the curly brackets around the zero.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by