- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Printing sentences using cell arrays
조회 수: 8 (최근 30일)
이전 댓글 표시
Given: 3 names, 3 verbs, 3 nouns; how to initalize a cell array.
Find: I need to print random elements from each of the above terms in a cell array to make a sentence.
Issue: I can initialize the cell arrays, (I think), but I am struggling to print the sentences in the command window...
My Solution: I initialize a cell array of 1 row and 3 columns, but I'm struggling to figure out how to randomize and print them.
C=cell(1,3);
Names={'Harry','Xavier','Sue'};
Verbs={'loves','eats','throws'};
Nouns={'baseballs','rocks','pizza'};
% I tried creating a variable rand, and stating rand=rand(C(Names, Verbs,
% Nouns)) such that it filled the empty cell array with random elements
% from the names, verbs, and nouns and got the error !!!Unable to use a value of type cell as an index.
% I'd then I think use the fprintf function to display the value of C?
댓글 수: 0
채택된 답변
Hassaan
2024년 4월 14일
편집: Hassaan
2024년 4월 14일
% Define cell arrays
Names = {'Harry', 'Xavier', 'Sue'};
Verbs = {'loves', 'eats', 'throws'};
Nouns = {'baseballs', 'rocks', 'pizza'};
% Generate and print a random sentence
fprintf('%s %s %s.\n', Names{randi(numel(Names))}, Verbs{randi(numel(Verbs))}, Nouns{randi(numel(Nouns))});
@Kyle Weaver By accessing the cell array elements directly in the fprintf statement, you can reduce the overhead of temporary variables.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
Feel free to contact me.
댓글 수: 9
Voss
2024년 4월 15일
You can also use ctrl+] or ctrl+[ to increase or decrease, respectively, the tab level of one or more lines of code.
추가 답변 (1개)
Voss
2024년 4월 14일
편집: Voss
2024년 4월 14일
Here's a way to generate multiple random sentences at once:
Names={'Harry','Xavier','Sue','Howard','Fred'}; % adding two names and one noun
Verbs={'loves','eats','throws'}; % to show that these don't all
Nouns={'baseballs','rocks','pizza','iPads'}; % have to be the same length
% number of sentences to print:
N = 12;
C = [ ...
Names(randi(end,[1 N])); ...
Verbs(randi(end,[1 N])); ...
Nouns(randi(end,[1 N])); ...
]
fprintf('%s %s %s.\n',C{:})
And here's one way to generate all possible sentences of the specified form:
T = combinations(Names,Verbs,Nouns)
C = table2array(T).';
fprintf('%s %s %s.\n',C{:})
댓글 수: 7
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!