Concatenating a cell array

15 次查看(过去 30 天)
Spaceman
Spaceman 2024-4-15
评论: Spaceman 2024-4-22
Given: A cell array with names where names={'Harry','Xavier','Sue'};
Find: How to concantenate a '1' at the end of each character array using a for-loop and the strcat function, among others.
Issue: I am utterly confused on what this is asking and why. I am assuming it's just practice concatenating but I have only really done this in excel.
My solution: Being unfamiliar with this in MATLAB, I don't really know where to start but I tried:
CAT=strcat(ones(Names));
% Got an error here: Size inputs must be numeric.
So If I can't use the ones function, how else can I approach this for that part of the question?
  4 个评论
Spaceman
Spaceman 2024-4-15
@Stephen23 I feel like the idiot who is pushing on a pull door when I code sometimes! XD
Thank you.
Spaceman
Spaceman 2024-4-15
@Aquatris Thank you for that clarification. I was trying to brute force the ones() function to do something it was not intended to do. I was literally pushing a sqaure peg into a round hole.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2024-4-15
The assignment tells you to use a for loop. So even if it can be solved without one, let's do it. But before we can do that, we need to figure out what we want to do in each iteration. That's easy: we want to do something with each element, so let's write a loop that helps us do that:
names = {'Harry','Xavier','Sue'};
for n=1:numel(names)
end
So now we have a loop. The next step is to add '1' to each element. While we could use square brackets, we can use the strcat function as well. If you don't know what to do, you need to read the documentation for this function:
help strcat % use doc instead of help to open the documentation browser
strcat - Concatenate strings horizontally This MATLAB function horizontally concatenates the text in its input arguments. Syntax s = strcat(s1,...,sN) Input Arguments s1,...,sN - Input text character arrays | cell array of character vectors | string arrays Examples openExample('matlab/ConcatenateTwoStringsExample') openExample('matlab/ConcatenateTwoCellArraysExample') openExample('matlab/ConcatenateTwoCellArrayswithScalarCellArrayExample') openExample('matlab/ConcatenateTwoStringArraysExample') See also append, plus, cat, vertcat, horzcat, cellstr, strjoin, join Introduced in MATLAB before R2006a Documentation for strcat doc strcat Other uses of strcat cell/strcat string/strcat
After reading this (in the documentation browser), you can simply adapt the example given:
strcat(names{n},'1')
ans = 'Sue1'
% note the {} to get the content of the cell
% () would give you the cell itself, not the contents
The final piece of the puzzle is to store it back in the original array:
names = {'Harry','Xavier','Sue'};
for n=1:numel(names)
names{n} = strcat(names{n},'1');
end
names
names = 1x3 cell array
{'Harry1'} {'Xavier1'} {'Sue1'}
If you want to be a smart-ass, you can satisfy the requirement of a for loop AND vectorize your code. Your teacher will either love you or hate you.
names = {'Harry','Xavier','Sue'};
for n=1
names = strcat(names,'1');
end
names
names = 1x3 cell array
{'Harry1'} {'Xavier1'} {'Sue1'}
  3 个评论
Stephen23
Stephen23 2024-4-16
"Is there a way to use a for-loop to concatenate the '1' at the end WITHOUT using the strcat function in the loop?"
Of course. This is MATLAB, which you should always remember is based on arrays, so some array operations could do something similar, e.g. indexing or concatenation:
names = {'Harry','Xavier','Sue'};
for k = 1:numel(names)
names{k}(end+1) = '1'; % indexing
names{k} = [names{k},'2']; % concatenation
end
names
names = 1x3 cell array
{'Harry12'} {'Xavier12'} {'Sue12'}
The name MATLAB comes from "MATrix LABoratory": thinking in terms of matrices and arrays helps to use MATLAB.
Spaceman
Spaceman 2024-4-22
Thank you, this is beautiful.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by