- 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
조회 수: 1 (최근 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에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!