Replace values in one column in multiple cells in array
4 次查看(过去 30 天)
显示 更早的评论
I'm trying to replace the 5th column in each cell in a cell array with the 5th column of each cell from another cell array. I made the following function, which does this but also replaces the values in all other columns with 0. How do I do this without deleting all other values from the other columns. The function is:
function [X]=replace_cells(cell)
X={};
for i=1:length(cell)
X{i}(:,[5])=cell{i}(:,[5]);
end
4 个评论
采纳的回答
Amos
2016-2-23
Maybe try:
function [X]=replace_cells(cell,X)
for i=1:length(cell)
X{i}(:,[5])=cell{i}(:,[5]);
end
2 个评论
Guillaume
2016-2-23
编辑:Guillaume
2016-2-23
Please replace the variable name cell with something that is not a matlab function. While it does not cause any problem in this short function, it is still confusing to the casual reader.
For that matter, using meaningful names for all variables would be much better. How about:
function destination = replace_cell(destination, source, column)
assert(size(destination) == size(source));
for cidx = 1:numel(source)
destination{cix}(:, column) = source{cidx}(:, column);
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!