Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a value to multiple cells in matlab

Tags:

cell

matlab

I have a 1D logical vector, a cell array, and a string value I want to assign.

I tried "cell{logical} = string" but I get the following error:

The right hand side of this assignment has too few values to satisfy
the left hand side.

Do you have the solution?

like image 828
Flavian Hautbois Avatar asked Jul 12 '12 16:07

Flavian Hautbois


People also ask

How do you call multiple elements from an array in Matlab?

Always specify the row first and column second. To refer to multiple elements of an array, use the colon ':' operator, which allows you to specify a range of elements using the form 'start:end'. The colon alone, without start or end values, specifies all the elements in that dimension.

What does deal () do in Matlab?

The deal function is an adapter that allows cell arrays and structure arrays to be cross assigned to each other. It gets its name from the metaphor of dealing a round of cards.

How do you define multiple variables in Matlab?

Use comma-separated lists to get multiple variables in the left hand side of an expression. You can use deal() to put multiple assignments one line. [x,y] = deal(cell(4,8), cell(4,8));


1 Answers

You don't actually need to use deal.

a = cell(10,1); % cell array
b = rand(1,10)>0.5; % vector with logicals
myString = 'hello'; % string

a(b) = {myString};

Looking at the last line: on the left hand side we are selecting a subset of cells from a and saying that they should all equal the cell on the right hand side, which is a cell containing a string.

like image 198
user2000747 Avatar answered Oct 14 '22 03:10

user2000747