- You have a cell array of scalars and row vectors.
- Read the cells in column-major order
- Reshape the cell contents to a column vector
- Concatenate to form one large column vector
How can I create a function of my code
5 次查看(过去 30 天)
显示 更早的评论
Hi, I got a code that I want to convert to a a function so I do not write for each variable the whole code. I do not really understand the documentation page because I have a code that is quite big and I get confused. My code is to generate the same variable without the doubles inside. The output that I want is the same name as the variable input. So that is where I got the problem. I post the code so you can see:
function y = removedbl(var)
numCells = numel(Time_msg_match); %Time_msg_match was the original variable for this code was created
Time_msg_match2 = zeros(numCells+10000, 1);
vector2Index = 1;
for k = 1 : numCells
len = length(Time_msg_match{k});
if len == 1
Time_msg_match2(vector2Index) = Time_msg_match{k};
vector2Index = vector2Index + 1;
else
fprintf('Row %d has %d elements in it.\n', k, length(Time_msg_match{k}));
for k2 = 1 : len
thisVector = Time_msg_match{k};
Time_msg_match2(vector2Index) = thisVector(k2);
vector2Index = vector2Index + 1;
end
end
end
if vector2Index < numCells
Time_msg_match2 = Time_msg_match2(1 : vector2Index - 1);
end
% fprintf('Original Time_msg_match had %d rows.\n', numCells) %Nos dice de que numero a que numero pasamos al quitar las celdas vacias
% fprintf('Afterwards Time_msg_match had %d rows.\n', numel(Time_msg_match2))
end
0 个评论
采纳的回答
DGM
2022-2-3
编辑:DGM
2022-2-3
This probably covers what you are trying to do. I assume your problem description is as follows:
aaa = {1,2,3,4,5,ones(1,5),2*ones(1,5),3*ones(1,5)};
bbb = removedbl(aaa)
ccc = vectorizecell(aaa)
% this is your function with some fixes
function Time_msg_match2 = removedbl(Time_msg_match)
numCells = numel(Time_msg_match); %Time_msg_match was the original variable for this code was created
Time_msg_match2 = zeros(numCells+10000, 1);
vector2Index = 1;
for k = 1 : numCells
len = length(Time_msg_match{k});
if len == 1
Time_msg_match2(vector2Index) = Time_msg_match{k};
vector2Index = vector2Index + 1;
else
fprintf('Row %d has %d elements in it.\n', k, length(Time_msg_match{k}));
for k2 = 1 : len
thisVector = Time_msg_match{k};
Time_msg_match2(vector2Index) = thisVector(k2);
vector2Index = vector2Index + 1;
end
end
end
if vector2Index < numCells+10000 % to fix padding
Time_msg_match2 = Time_msg_match2(1 : vector2Index - 1);
end
end
% this is an alternative method to do the same
function outvec = vectorizecell(inarray)
isrv = cellfun(@isrow,inarray);
if ~all(isrv(:))
error('VECTORIZECELL: some cells do not contain row vectors')
end
inarray = cellfun(@(x) x(:),inarray,'uniform',false);
outvec = cell2mat(inarray(:));
end
If the restriction to "row vectors only" does not apply, you can remove the first few lines of vectorizecell(). Matrix contents will be vectorized in column-major order. If you want it to handle these cases in row-major order, then let me know.
3 个评论
DGM
2022-2-3
I don't understand what you're asking. Fom the perspective of the calling scope, the variable passed to the function is not changed.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!