How to extract Curly Braces Vector from Cell String in Matlab - string

I have the following string (char of 1X48) in cell in Matlab
{ {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } }.
I am trying to get three separate strings in new line with just space, so that data will look like
1 0 0
0 1 0
0 0 1
I will really appreciate if anyone has any idea to covert in matlab.
Thanks

Better to use cell2mat function
In your case you can try something like this,
temp = { {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } };
out = [cell2mat(temp{1, 1}); cell2mat(temp{1, 2}); cell2mat(temp{1, 3})]
I hope it will help!!

You are looking for strjoin:
a = { {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } };
b = strjoin(a, ' ')
Then:
b =
1 0 0 0 1 0 0 0 1
Edit: if you want to get new lines involved, you can use
b = [strjoin(a(1), ' '); strjoin(a(2), ' '); strjoin(a(3), ' ');]
Then:
b =
1 0 0
0 1 0
0 0 1
P.S.: strjoin works in MATLAB R2013b. For earlier versions, you can download the strjoin function from here.

Related

How do I view the PDF file downloaded from docusign?

I am downloading the signed documents using the docusign getDocument API, but the downloaded file is coming in a weird format(json as per the Content-type header). What format is this and how can I convert it to a viewable/readable format.
Here is a snippet from the response -
%PDF-1.5\n%ûüýþ\n%Writing objects...\n4 0 obj\n<<\n/Type /Page\n/Resources 5 0 R\n/Parent 3 0 R\n/MediaBox [0 0 595.32000 841.92000 ]\n/Contents [6 0 R 7 0 R 8 0 R ]\n/Group <<\n/Type /Group\n/S /Transparency\n/CS /DeviceRGB\n>>\n/Tabs /S\n/StructParents 0\n>>\nendobj\n5 0 obj\n<<\n/Font <<\n/F1 9 0 R\n/F2 10 0 R\n/F3 11 0 R\n/F4 12 0 R\n/F5 13 0 R\n>>\n/ExtGState <<\n/GS7 14 0 R\n/GS8 15 0 R\n>>\n/ProcSet [/PDF /Text /ImageB /ImageC /ImageI ]\n/XObject <<\n/X0 16 0 R\n>>\n>>\nendobj\n2 0 obj\n<<\n/Producer (PDFKit.NET 21.1.200.20790)\n/CreationDate (D:20210429103256-07'00')\n/ModDate (D:20210429103256-07'00')\n/Author ()\n/Creator ()\n/Keywords <>\n/Subject ()\n/Title ()\n>>\nendobj\n6 0 obj\n<<\n/Length 4\n>>\nstream\n\n q \nendstream\nendobj\n7 0 obj\n<<\n/Filter /FlateDecode\n/Length 7326\n>>\nstream\nxœ½]ëo\u001c7’ÿnÀÿCã\u0016¸›YDm¾šd\u0007A\u0000É’\u001dçüZ[Þà\u0010ï\u0007Å’ma-É‘FÉæ¿?
What language are you using, and are you using one of the DocuSign SDKs or a raw API call?
When I make a GetDocument call (specifically {{vx}}/accounts/{{accountid}}/envelopes/{{envelopeId}}/documents/combined, for example), the response headers have Content-Type: application/pdf and Content-Dispostion: file; filename=file.pdf, and the body of the response is the binary of the PDF file itself.
A snippet of mine begins with this:
%PDF-1.5
%����
%Writing objects...
4 0 obj
<<
/Type /Page
/Parent 3 0 R
/Resources 10 0 R
/MediaBox [0 0 612 792 ]
/Contents [11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 17 0 R ]
/Group <<
/Type /Group
/S /Transparency
/CS /DeviceRGB
So it looks like whatever system you have picking up the response is including \n newlines and potentially other control characters.
You'll want to look at how your tools handle the API response: if you can dump the raw output from DocuSign to a PDF file, that would work, but with the extra formatting being injected it's no longer valid.
I used this piece of code to save the response from DocuSign getDocument API to a PDF document-
envelopesApi.getDocument(ACCOUNT_ID, envelope_id, 'combined', function (error, document, response) {
if (document) {
var filename = ‘docusign.pdf';
fs.writeFile(filename, new Buffer(document, 'binary'), function (err) {
if (err) console.log('Error: ' + err);
});
}
});

Coin Chane - Dynamic Programming - How read all solutions from the DP table

I have seen different solutions to the same problem, but none of them seem to use the approach I used. So here I'm trying to solve the classical coin-change problem in a bottom up dynamic programming approach using a DP table.
int[][] dp = new int[nums.length+1][target+1];
for (int i = 0; i <= nums.length; ++i)
dp[i][0] = 1;
for (int i = 1; i < dp.length; ++i) {
for (int j = 1; j < dp[i].length; ++j) {
if (nums[i-1] <= j)
dp[i][j] = dp[i-1][j] + dp[i][j-nums[i-1]];
else
dp[i][j] = dp[i-1][j];
}
}
The above code generates the table. For fun if I have: {2,3,5} coins, and want to exchange 8, the table would look like:
1 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1
1 0 1 1 1 1 2 1 2
1 0 1 1 1 2 2 2 3
For the above the following method seem to be working well:
current <- 4
while (current > 0) do
i <- current
j <- 8
while (j > 0) do
if (dp[i][j] != dp[i-1][j]) then
nums[i-1] is part of the current solution
j <- j-1
else
i <- i-1
end
end
current <- current-1
end
Walking through for the above example, we get the following solutions:
1) [5,3]
2) [3,3,2]
3) [2,2,2,2]
Which is great! At least I thought, until I tried: {1,2} with a T=4. For this the table looks like:
1 0 0 0 0
1 1 1 1 1
1 1 2 2 3
With the above algorithm to print all solutions, I only get:
[2,2]
[1,1,1,1]
Which means I won't recover [2,1,1]. So this question is not about the generic how to print the solutions for different approaches to this problem, but how can I read the above DP table to find all solutions.
Well I have one solution but sure... I can see why the other similar answers are using different approaches for this problem. So the algorithm to generate all the possible answers from the above table looks like:
private List<List<Integer>> readSolutions(int[] nums, int[][] dp, int currentRow, int currentColumn, List<Integer> partialSolution) {
if (currentRow == 0) {
return new ArrayList<>();
}
if (currentColumn == 0) {
return new ArrayList<List<Integer>>(Collections.singleton(partialSolution));
}
List<List<Integer>> solutions = new ArrayList<>();
if (dp[currentRow][currentColumn] != dp[currentRow-1][currentColumn]) {
List<Integer> newSolution = new ArrayList<>(partialSolution);
newSolution.add(nums[currentRow-1]);
solutions.addAll(readSolutions(nums, dp, currentRow, currentColumn-nums[currentRow-1], newSolution));
solutions.addAll(readSolutions(nums, dp, currentRow-1, currentColumn, partialSolution));
return solutions;
}
return readSolutions(nums, dp, currentRow-1, currentColumn, partialSolution);
}
The logic on the other hand is simple. Take the above table for example:
0 1 2 3 4
0 1 0 0 0 0
1 1 1 1 1 1
2 1 1 2 2 3
To get a single solution we start from the bottom right corner. If the value does match with the value directly above us, we move up. If it doesn't we move left by the amount corresponding to the row we're in. To generate all answers on on the other hand from the above table...
we're at some position (i,j)
if the value at (i-1,j) is the same as (i,j) we make a recursive call to (i-1,j)
if the values do not match, we have 2 choices...
we can use the number corresponding to the current row, and recurse into (i,j-n)
we can skip the number and check if we can create (i,j) instead by using a recursive call to (i-1,j) instead.
if we reach the first row, we return an empty list
if we reach the first column, we return what we have already found, which will have the sum of target.
Looks awful right, but works as expected.

replace numbers smaller than certain threshold with zero

I have a large data sheet (see below) in .csv format. I want to replace the numbers in each column and row with zero if it is smaller than a certain value, let's say 0.1.
Could anyone give me a hand? Thanks a lot.
I guess it can be done with sed as in this example
BCC_ACR_CR BCC_ACR_CU BCC_ACR_FE BCC_ACR_MN BCC_ACR_MO
0.2826027 3.89E-12 0.58420346 2.23E-13 0.2105587
0.27986588 3.80E-12 0.58501168 2.27E-13 0.20890705
0.27986588 3.80E-12 0.58501168 2.27E-13 0.20890705
0.27986588 3.80E-12 0.58501168 2.27E-13 0.20890705
0.28038733 3.81E-12 0.58196375 5.88E-05 0.21239142
0.26855376 3.27E-12 0.60364524 2.06E-13 0.11205138
0.27220042 3.28E-12 0.60349573 2.08E-13 0.11530944
0.36294869 3.14E-12 0.50515464 1.64E-13 3.12E-12
0.36294869 3.14E-12 0.50515464 1.64E-13 3.12E-12
0.40837234 3.07E-12 0.47202708 1.73E-13 3.03E-12
0.3643896 3.25E-12 0.50431576 1.63E-13 3.14E-12
0.3643896 3.25E-12 0.50431576 1.63E-13 3.14E-12
0.35885258 3.21E-12 0.50978952 1.64E-13 3.12E-12
Here is one for GNU awk. Field separator is assumed to be a run of space so empty fields are not allowed:
$ gawk -v value=0.1 ' # give treshold values as parameter
BEGIN { RS="( +\n?|\n)" } # every field is considered a record
{
ORS=RT # RT stores actualized RS
if($0<value) # comparison
$0=sprintf("0%-" length()-1 "s","") # format data to fit the field
}1' file # output
Output:
BCC_ACR_CR BCC_ACR_CU BCC_ACR_FE BCC_ACR_MN BCC_ACR_MO
0.2826027 0 0.58420346 0 0.2105587
0.27986588 0 0.58501168 0 0.20890705
0.27986588 0 0.58501168 0 0.20890705
0.27986588 0 0.58501168 0 0.20890705
0.28038733 0 0.58196375 0 0.21239142
0.26855376 0 0.60364524 0 0.11205138
0.27220042 0 0.60349573 0 0.11530944
0.36294869 0 0.50515464 0 0
0.36294869 0 0.50515464 0 0
0.40837234 0 0.47202708 0 0
0.3643896 0 0.50431576 0 0
0.3643896 0 0.50431576 0 0
0.35885258 0 0.50978952 0 0

MDP Policy Plot for a Maze

I have a 5x-5 maze specified as follows.
r = [1 0 1 1 1
1 1 1 0 1
0 1 0 0 1
1 1 1 0 1
1 0 1 0 1];
Where 1's are the paths and 0's are the walls.
Assume I have a function foo(policy_vector, r) that maps the elements of the policy vector to the elements in r. For example 1=UP, 2=Right, 3=Down, 4=Left. The MDP is set up such that the wall states are never realized so policies for those states are ignored in the plot.
policy_vector' = [3 2 2 2 3 2 2 1 2 3 1 1 1 2 3 2 1 4 2 3 1 1 1 2 2]
symbols' = [v > > > v > > ^ > v ^ ^ ^ > v > ^ < > v ^ ^ ^ > >]
I am trying to display my policy decision for a Markov Decision Process in the context of solving a maze. How would I plot something that looks like this? Matlab is preferable but Python is fine.
Even if some body could show me how to make a plot like this I would be able to figure it out from there.
function[] = policy_plot(policy,r)
[row,col] = size(r);
symbols = {'^', '>', 'v', '<'};
policy_symbolic = get_policy_symbols(policy, symbols);
figure()
hold on
axis([0, row, 0, col])
grid on
cnt = 1;
fill([0,0,col,col],[row,0,0,row],'k')
for rr = row:-1:1
for cc = 1:col
if r(row+1 - rr,cc) ~= 0 && ~(row == row+1 - rr && col == cc)
fill([cc-1,cc-1,cc,cc],[rr,rr-1,rr-1,rr],'g')
text(cc - 0.55,rr - 0.5,policy_symbolic{cnt})
end
cnt = cnt + 1;
end
end
fill([cc-1,cc-1,cc,cc],[rr,rr-1,rr-1,rr],'b')
text(cc - 0.70,rr - 0.5,'Goal')
function [policy_symbolic] = get_policy_symbols(policy, symbols)
policy_symbolic = cell(size(policy));
for ii = 1:length(policy)
policy_symbolic{ii} = symbols{policy(ii)};
end

OCAML counting frequency of char using switches

I am trying to count the amount of time each char appears in a string, I'm using switches and a for loop, however, they are not being incremented properly. Here is my code
let countChar x =
match x with
'A'-> countA := !countA +1;
|'C'-> countC := !countC +1;
|'T'-> countT := !countT +1;
|'G'-> countG := !countG +1;
;;
let demoStri = "ACGTACGT" in
for j = 0 to 7 do
countChar demoStri.[j];
let tempA = !countA in
print_int tempA;
print_string "\n";
let tempC = !countC in
print_int tempC;
print_string "\n";
let tempG = !countG in
print_int tempG;
print_string "\n";
let tempT = !countT in
print_int tempT;
print_string "\n";
done
But for some reason it's only incrementing 1, and it returns 1 0 0 0, 2 0 0 0, 3 0 0 0 and so on..... I was wondering if something went wrong in the process?
I see no problem with this code in its current form, it works for me. You don't show your initializations of countA, countC, countT, and countG, but if I initialize as follows:
let countA = ref 0
let countC = ref 0
let countT = ref 0
let countG = ref 0
Then run your code I get this series of numbers (collapsed four to a line to save space):
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
2 1 1 1
2 2 1 1
2 2 2 1
2 2 2 2
The biggest problem here is that you are using the tempH variable to index the string instead of the j you should have been using.
let () =
let demoStri = "ACGTACGT" in
let countA = ref 0 in
let countC = ref 0 in
let countT = ref 0 in
let countG = ref 0 in
for j = 0 to String.length demoStri - 1 do
match demoStri.[j] with
| 'A'-> countA := !countA +1
| 'C'-> countC := !countC +1
| 'T'-> countT := !countT +1
| 'G'-> countG := !countG +1
| _ -> assert false
done;
print_int !countA; print_string "\n";
print_int !countC; print_string "\n";
print_int !countT; print_string "\n";
print_int !countG; print_string "\n"

Resources