Replace cells in one cell variable in Matlab [duplicate] - matlab

This question already has answers here:
MATLAB: copy a specific portion of an array
(2 answers)
Closed 7 years ago.
I have one variable (1X30 cell) and I want to replace cells 21 through 30 with cells 21 through 30 from another variable (also 1X30 cell). Can someone help?

You should check out the documentation on cells and on indexing. This is quite a simple task:
C1(21:end) = C2(21:end)
or
C1(21:30) = C2(21:30)

Related

Find and count matching cell elements [duplicate]

This question already has answers here:
How do I find a specific cell within a cell array?
(2 answers)
Closed 6 years ago.
I have a cell like
A= {[7,7]; [7,4,7]; [7,7]; [7,7]; [4,5]};
I want to count the number of [7,7] elements.
How can I do this in MATLAB without using a loop?
Thanks.
You could use cellfun with isequal
sum(cellfun(#(x) isequal(x, [7 7]), A))
ans =
3

MATLAB: How to randomize the rows of a matrix? [duplicate]

This question already has answers here:
Random order of rows Matlab
(4 answers)
Closed 7 years ago.
I'm looking for an efficient way to manipulate a 40x151 matrix so that the rows are randomly scrambled.
I worked out the answer just as I was about to post.
new_matrix = old_matrix(randperm(40),:)

How to create a list of lists with unequal number of elements in matlab? [duplicate]

This question already has an answer here:
How to store this structure (list of lists of integers) in Matlab?
(1 answer)
Closed 7 years ago.
I want to create list of the following type:
[ [1,2,3], [5,6] , [1,2,4,6,7,89,9,74,4]]
But whenever I do, matlab automatically concatenates the list. Can anyone help me out?
What you want is called cell array, you can create one by doing:
A = {[1,2,3], [5,6], [1,2,4,6,7,89,9,74,4]};
To access cell elements you have to use the {} operator. As #TroyHaskin said, you can find a most complete answer here

how to rearrange each block into a column vector? [duplicate]

This question already has answers here:
Efficient Implementation of `im2col` and `col2im`
(2 answers)
Closed 7 years ago.
example: I have an image,it's size 512X512pixel,then i have splited it into 8x8 blocks.Now i would have 64x64 blocks.Now how to rearrange each block 8x8 into column vector so that the dimension would be 64x4096pixel without inbuilt function "im2col".please help me out.
Thanks.
x=rand(512,512);
xi=mat2cell(x,8*ones(1,64),8*ones(1,64));
xii=cellfun(#(x)reshape(x,1,64),xi,'UniformOutput',false);
y=cell2mat(xii);

How to set a number in a couple of cells within a cell array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Assign a value to multiple cells in matlab
I am trying to enter a number, lets say 3, into all the cells in column 2 that are empty.
Like this:
emptyList = cellfun(#isempty,anscell)
anscell{emptyList(:,2),2}=3
but I get this message that
The right hand side of this assignment has too few values to satisfy the left hand side.
Can I overcome it without loops and creating sum and ones functions?
Is this what you need?
anscell = cell(3,2)
emptyList = cellfun(#isempty,anscell)
anscell(emptyList(:,2),2)={3}
Does this do what you want to do?
[anscell{emptyList(:,2),2}] = deal(3)