Concatenating a cell array

11 vues (au cours des 30 derniers jours)
Spaceman
Spaceman le 15 Avr 2024
Commenté : Spaceman le 22 Avr 2024
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 commentaires
Spaceman
Spaceman le 15 Avr 2024
@Stephen23 I feel like the idiot who is pushing on a pull door when I code sometimes! XD
Thank you.
Spaceman
Spaceman le 15 Avr 2024
@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.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 15 Avr 2024
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 commentaires
Stephen23
Stephen23 le 16 Avr 2024
"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 le 22 Avr 2024
Thank you, this is beautiful.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by