필터 지우기
필터 지우기

Concatenating a cell array

조회 수: 8 (최근 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