Reputation: 1865
For
A=[100;300;1000;240]
and
B=cell(8,1)
I have the following results stored in a B
[100]
[300]
[1000]
[240]
[100;300;240]
[100;1000]
[300;1000]
[100;300;1000]
I want to print these to display the output as :
choose first
choose second
choose third
choose fourth
choose first or second or fourth
choose first or third
.
.
etc
Basically, from the array A=[100;300;1000;240]
, I want each value inside of it to be represented by a string, and not one variable. Any idea how to do this ?
note :
For my code, I want the user to input their own numbers in array A, and hence the length of A is variable and can be more than 4. The size of cell B also changes according to a formula, so it is not always fixed at size 8.
I would also appreciate a simple code, nothing too complex (unless necessary) as I don't have professional knowledge with matlab. A simpler code can help me understand and learn.
Upvotes: 0
Views: 497
Reputation: 814
for mapping I would just use a map object
index_to_string = containers.Map(keySet,valueSet)
where
keySet = 1:20
valueSet = {'first'; 'second'; ...; 'twentieth'}
If A
is available before printing, you can use the same valueSet
, just cut it down to the size of A
.
index_to_string = containers.Map(A,valueSet(1:length(A)))
Example:
G = cell(size(B))
for i = 1:length(B)
out1 = 'choose ';
if len(B{i}) == 1
out1 = [out1, index_to_string(B{i})];
else
temp = B{i}
for j=1:(length(temp)-1)
out1 = [out1, index_to_string(temp(j)), ' or ' ];
end
out1 = [out1, index_to_string(temp(end))];
end
G{i} = out1
end
Upvotes: 1
Reputation: 9075
This answer is entirely based on JaredS's
answer. I have just clarified your doubts.
Write this in some m-file.
Choices=A; Results=B;
%simple boolean to indicate whether a choice has been made already
answerChosen = 0;
for k = 1:length(Results)
Response = 'choose';
for m = 1:length(Choices)
if any(Results{k} == Choices(m))
if answerChosen
Response = [Response ' or ' NumToOrd(m)];
else
answerChosen = 1;
Response = [Response ' ' NumToOrd(m)];
end
end
end
fprintf('%s\n',Response);
answerChosen = 0;
end
Please write the following function in a separate file and put that in the same directory as the previous m-file. Then you should get an error saying: "Undefined function 'NumToOrd' for input arguments of type 'double'."
function ordinal = NumToOrd(number)
switch number
case 1, ordinal = 'first';
case 2, ordinal = 'second';
case 3, ordinal = 'third';
case 4, ordinal = 'fourth';
otherwise, ordinal = 'out of index';
end
Upvotes: 0
Reputation: 53
Here's how I'd do it
function IChooseYouPikachu(Choices, Results)
% put in A for choices and B for results
%simple boolean to indicate whether a choice has been made already
answerChosen = 0;
for k = 1:length(Results)
Response = 'choose';
for m = 1:length(Choices)
if any(Results{k} == Choices(m))
if answerChosen
Response = [Response ' or ' NumToOrd(m)];
else
answerChosen = 1;
Response = [Response ' ' NumToOrd(m)];
end
end
end
fprintf('%s\n',Response);
answerChosen = 0;
end
function ordinal = NumToOrd(number)
switch number
case 1, ordinal = 'first';
case 2, ordinal = 'second';
case 3, ordinal = 'third';
case 4, ordinal = 'fourth';
otherwise, ordinal = 'out of index';
end
Upvotes: 0