Modify elements in arrays stored in cells at specified rows/columns

29 views (last 30 days)
Hi, I have a 2d cell array, where each cell contains a 1x2 double,
E.g. X = { [1,0], [1,0], [1,0], [1,0]; [1,0], [1,0], [1,0], [1,0] };
I want to assign the value 1 to the second element in each cell at row r=2, columns c=1:2. Because my actual data is large I want to do this without a for loop. In concept something like:
cellfun( @(c) (X{r,c}(2) = 1), 1:2 );
Thanks in advance

Accepted Answer

Michael Van de Graaff
Michael Van de Graaff on 22 Mar 2022
I modified X to make it a bit clearer where each element is going, but I believe this does what you want:
X = { [1,0.1], [2,0.2], [3,0.3], [4,0.4]; [11,0.11], [12,0.12], [13,0.13], [14,0.14] };
szeX = size(X);
y = cell2mat(X);
y =reshape(y',[],2);
y(2,:) = 1;
y = reshape(y,[],2)';
szy = size(y);
cellsize = size(X{1});
d1 = cellsize(1)*ones(szeX(1),1);
d2 = cellsize(2)*ones(1,szeX(2));
newX = mat2cell(y,d1,d2)
cell2mat(newX)
cell2mat(X)
The last two lines just for comparison

More Answers (1)

Image Analyst
Image Analyst on 22 Mar 2022
Your cells do not contain arrays with two rows. Look
X = { [1,0], [1,0], [1,0], [1,0]; [1,0], [1,0], [1,0], [1,0] }
X = 2×4 cell array
{[1 0]} {[1 0]} {[1 0]} {[1 0]} {[1 0]} {[1 0]} {[1 0]} {[1 0]}
Your cells contain a 1-row row vector with 2 columns.
But let's say you did have two ross in the arrays in the cells. I think the best way is to use a for loop:
X = { randi(9, 4, 3), randi(9, 4, 3), randi(9, 4, 3), randi(9, 4, 3);
randi(9, 4, 3), randi(9, 4, 3), randi(9, 4, 3), randi(9, 4, 3)}
X = 2×4 cell array
{4×3 double} {4×3 double} {4×3 double} {4×3 double} {4×3 double} {4×3 double} {4×3 double} {4×3 double}
% Show the starting values:
celldisp(X)
X{1,1} = 7 2 6 2 9 1 5 5 3 4 3 3 X{2,1} = 6 4 1 9 2 6 9 1 5 7 6 8 X{1,2} = 6 6 9 6 5 5 2 8 1 6 8 4 X{2,2} = 7 9 5 2 5 1 1 4 1 6 5 1 X{1,3} = 5 9 9 5 9 8 1 9 5 5 4 3 X{2,3} = 3 6 7 2 6 6 9 9 2 9 1 7 X{1,4} = 7 1 6 8 4 2 8 3 8 5 9 6 X{2,4} = 7 9 7 7 3 3 7 6 8 4 4 5
for k = 1 : numel(X)
% Get cell contents.
thisCellContents = X{k};
% Change it.
thisCellContents(2, 1:2) = [1, 2];
% Put it back into the cell.
X{k} = thisCellContents;
end
% See the result:
celldisp(X)
X{1,1} = 7 2 6 1 2 1 5 5 3 4 3 3 X{2,1} = 6 4 1 1 2 6 9 1 5 7 6 8 X{1,2} = 6 6 9 1 2 5 2 8 1 6 8 4 X{2,2} = 7 9 5 1 2 1 1 4 1 6 5 1 X{1,3} = 5 9 9 1 2 8 1 9 5 5 4 3 X{2,3} = 3 6 7 1 2 6 9 9 2 9 1 7 X{1,4} = 7 1 6 1 2 2 8 3 8 5 9 6 X{2,4} = 7 9 7 1 2 3 7 6 8 4 4 5
See? the first and second columns of the second row of each array get changed to 1 and 2 respectively.
Now I know you didn't want a for loop, probably for speed and efficiency, but let's face it - you threw away all possibility of getting speed and efficiency when you decided to use a cell array in the first place.
And it's a myth that cell arrays are horribly slow. Many years ago they've been speeded up a great deal. How many elements are in your cell array anyway? Hundred of millions or billions? For a few million I doubt you'd notice any difference. Any slowness would be caused not by the for loop but by your decision to use a cell array. Look, here is 10 million for loop iterations where it basically does nothing but iterate the for loop:
tic
for k = 1 : 1e7
;
end
toc
Elapsed time is 0.010463 seconds.
How many iterations would you do? See? The for loop alone is not the problem. Takes only a hundredth of a second. It's the data structure and what operations you do that is what eats up the time, not the for loop.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!