Please help translate c++ into matlab code!

Can anyone help me translate this c++ code into matlab code? I can't seem to get the hang of using string in matlab compared to c++, been sitting around it for ages now. Edit: the program I'm trying to do is that it outputs all possibilities to put + or − or nothing between the numbers from 1 to 9 (in ascending order) such that the result is some target number. The target number is entered by the user. Say for target number 100, one of the possibilities is: 1+2+3−4+5+6+78+9=100.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
# include <iomanip>
# include <cmath>
# include <fstream>
# include <cstring>
using namespace std;
int main()
{
int TARGET;
cin >> TARGET;
int i, total;
vector<string> all = { "1" }; // Will hold all 3^8 legitimate combinations as a string
for ( i = 2; i <= 9; i++ ) // Add each following digit with either -, + or nothing
{
char c = (char)( '0' + i );
vector<string> temp;
for ( string s : all ) // For each previous string there will be three new ones
{
temp.push_back( s + '-' + c );
temp.push_back( s + '+' + c );
temp.push_back( s + c );
}
all = temp;
}
// Now evaluate results by stringstreaming +n or -n into integers
vector<string> matches;
for ( string s : all )
{
stringstream ss( s );
ss >> total;
while ( ss >> i ) total += i;
if ( total == TARGET ) matches.push_back( s );
}
// Write results
cout << "There are " << matches.size() << " possibilities:\n";
for ( string s : matches ) cout << s << '\n';
}

1 Comment

Instead of pasting the code, it would be much better if you can explain what you are trying to do in this code. Understanding someone else's code requires much more effort. In the end, it will not be a line to line translation anyway. MATLAB will usually be able to do it in a few lines.

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 19 Apr 2020
Edited: Ameer Hamza on 19 Apr 2020
Try this. It tries all combinations of the symbols to find the required ones
clear
nums = num2str((1:9)')'; % '1234...9'
symbols = '+ -'; % space is used for empty symbol, it will be later ignored
x = repmat({symbols}, 1, 8); % total number of symbols are 8
symbols_combs = char(combvec(x{:})'); % create combinations of all symbols
exprs(:,2:2:16) = symbols_combs; % all possible expressions
exprs(:,1:2:17) = repmat(nums, size(exprs,1), 1);
exprs = mat2cell(exprs, ones(size(exprs,1),1), size(exprs,2));
exprs = cellfun(@(x) {strrep(x, ' ', '')}, exprs);
values = cellfun(@(x) str2num(x), exprs); % convert the char array to values
mask = values == 100;
final_exprs = exprs(mask);
Result:
>> final_exprs
final_exprs =
11×1 cell array
{'1+23-4+56+7+8+9' }
{'12+3-4+5+67+8+9' }
{'1+2+3-4+5+6+78+9'}
{'1+2+34-5+67-8+9' }
{'12-3-4+5-6+7+89' }
{'123-45-67+89' }
{'12+3+4+5-6-7+89' }
{'123+4-5+67-89' }
{'123+45-67+8-9' }
{'123-4-5-6-7+8-9' }
{'1+23-4+5+6+78-9' }

1 Comment

You can add breakpoints and go through the code line by line. Also read the documentation of the functions involved in this code. It will help you to understand the purpose of each line.

Sign in to comment.

More Answers (0)

Asked:

on 19 Apr 2020

Commented:

on 19 Apr 2020

Community Treasure Hunt

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

Start Hunting!