Redefining values within cell array using cellfun

6 次查看(过去 30 天)
Suppose I have a cell array of matrices, and I want to define certain elements of the arrays of each cell as a value, and then return those values to the corresponding elements of that matrix.
Would it be possible to do this using cellfun to operate on each cell? The below example highlights what I'm aiming for.
I have cell array C of arrays. I want to replace the elements 1:x:end of the array in each cell in C with 1. However, doing this element-by-element would be impractical, as would looping. I want all other elements to remain the same.
  1 个评论
Evan
Evan 2012-2-3
Just to note, I'm running into this problem due to the fact that cellfun doesn't support explicit assignments.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2012-2-3
function x = assignat(x, L, v)
x[L] = v;
end
newC = cellfun( @(T) assignat(T, 1:x:length(T), 1), C );
Note, though, that this is really just hiding a "for" loop, and that it would be expected that other forms would be faster, such as
newC = C;
for K = 1 : numel(C)
newC{K}(1:x:end) = 1;
end
I think it might be possible to code the cellfun() without using the auxiliary user function "assignat" that I invented, but the expressions would get complicated and would involve calls to several MATLAB functions -- no time saved.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by