Assign value zero to n elements of cell array

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일

13 개 추천

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

Karl
Karl 2014년 3월 18일
Excellent, thank you Sean. Just when I thought I had exhausted all combinations of brackets :)
Thank you for the concise answer and for explaining the reasoning behind it.
chris oj
chris oj 2018년 10월 18일
This answer just helped me...thanks Sean.
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?
@Flavia something like
myCell(2695:2746, 1) = {1}

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Parna Bhattacharya
Parna Bhattacharya 2018년 3월 17일

1 개 추천

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일

0 개 추천

Nice! Thanks (Had the same issue) Never thought about using the curly brackets around the zero.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2014년 3월 17일

댓글:

2019년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by