I'm working with GUIDE on MATLAB. In my GUI, there is a text field in which the user is to enter an array of numbers. How can I convert this into a form that can be used to perform calculations? Basically my question is how can a string like "[1 1 1]" be converted into an array? (I'm working with integers from the range of 0-360 so string-'0' doesn't work for me.)
You can use this:
a = str2num('[1 1 1]')
Not sure what you meen by "string-'0' doesn't work for me" though.
Related
I am having problems converting XML data into the appropriate data types using a function.
I have read in an XML file using XmlParse().
Within that there is an array which I loop around. <Cfloop array=#i.Task# index="t">
My understanding is that the items in this array are XML text. I can display all the items with CFoutput no problem. One item in the array (BaseLineColor) is a colour. #t.BaseLineColor# However this colour value is a single decimal integer number of varying length. I have worked out the maths to convert this decimal number into R,G,B decimal values. All good so far.
The problem is that if I try mathematical functions on BaseLineColor, then I get:
The value ?xml version="1.0" encoding="UTF-8"? BaseLineColor 255 /BaseLineColor cannot be converted to a number.
So OK I have tried a few methods to try and convert BaseLineColor to an integer but nothing works. Val() doesn't work. In fact I can't seem to convert it into any datatype.
For example, here is me trying to make it a string - same error:
<cfscript>
Strbaselinecolor=toString(t.BaseLineColor);
rdec=floor(Strbaselinecolor / 65536);
gdec=floor((Srtbaselinecolor - rdec * 65536)/256);
bdec=floor(Strbaselinecolor - rdec * 65536 - gdec * 256);
writeOutput("#t.baselinecolor#: #Strbaselinecolor# red #rdec#, green #gdec#, blue #bdec#")
</cfscript>
What function should I be using? Am I supposed to be pre-processing the XML in some way before I can refer to some of these values as integers?
There are a lot of values in the XML data which are numbers (some integers and some floating point numbers) and so it is not just about these items that are colours but a more general problem with using any XML data that is not text. I have tried to find some reference material on this but have not found anything relevant so far. Yet I'm guessing this is a common issue when reading in XML files.
Thanks in advance for any help.
The error message is correct. The code is trying perform mathematical operations on something that is not a number, despite the fact that it may appear to be one in a browser... You're probably getting tripped up by how browsers handle tag based code like xml.
This code (incorrectly) shows the value of t.BaseLineColor as a simple number 255
<cfscript>
t = xmlParse('<?xml version="1.0" encoding="UTF-8"?><BaseLineColor>255</BaseLineColor>');
writeOutput(t.BaseLineColor);
</cfscript>
Runnable Example
However, using a browser's "Inspect Element" tool, reveals the value is actually an xml string. Since browsers treat anything enclosed in < and > as html tags, which aren't rendered, only the number 255 is visible on screen.
writeDump() is more helpful here. It shows t.BaseLineColor as an xml node, and it's value is accessible through the xmlText attribute.
That simple value can be used in mathematical operations.
<cfscript>
t = xmlParse('<?xml version="1.0" encoding="UTF-8"?><BaseLineColor>255</BaseLineColor>');
result = t.BaseLineColor.xmlText / 65536 ;
writeOutput( result );
</cfscript>
Runnable Example
I am trying to do a sumproduct such as
=SUMPRODUCT(A1:A8,{8,7,6,5,4,3,2,1})
but it give #VALUE! error,
While it is gives a valid value for
=SUMPRODUCT({8,7,6,5,4,3,2,1},{8,7,6,5,4,3,2,1})
Or
=SUMPRODUCT(A1:A8,A1:A8)
According to my understanding it only works if all inputs are of Range or Array type, but not when there are both. Is there any way to make it work?
or you can use ; for vertical array :
=SUMPRODUCT(A1:A8,{8;7;6;5;4;3;2;1})
{8,7,6,5,4,3,2,1} is horizontal array similar to horizontal range like A1:H1 which will not work well with the vertical range A1:A8
I can't test it, but another way can be with the Row function:
=SUMPRODUCT(A1:A8,9-Row(A1:A8))
=SUMPRODUCT(TRANSPOSE(D3:D10),{8,7,6,5,4,3,2,1})
EDIT: Above code works if entered as array forumla
=SUMPRODUCT(D3:D10,TRANSPOSE({8,7,6,5,4,3,2,1}))
works without entering as array formula.
In Excel, I have a cell containing a string that looks like this:
[[1.234,3.21],[2.345,3.21]]
I want the average of 1.234 and 2.345 in one cell, and the average of 3.21 and 3.21 in the other.
I've never worked with a string that looks like an array from other programming languages before.
How can I achieve what I want with an Excel formula?
You could use the following:
=AVERAGE(--TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"[",""),"]",""),",",REPT(" ",99)),((COLUMN(A:A)-1)*99)+1,99)),--TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"[",""),"]",""),",",REPT(" ",99)),((COLUMN(B:B))*99),99)))
Then drag across one and down as far as needed
Use a regular expression for this.
Since u did not ask in a specific programming language, this is all I can answer.
I am still new in using MATLAB. I am trying to create a feed-forward neural network with MATLAB custom function feedforwardnet, I got my own training set with dimension 2 x 100, which is a multiple-input array.
Now I ran into the problem of how to feed this array into the feedforwardnet function. I tried
[NNINputs, NNTargets] = [n_xk, target] ;
where both n_xk and target are 2 x 100 "double" arrays. It shows the error:
??? Too many output arguments.
I also tried wrapping the array into a cell array as follows
myTrainSet = num2cell([n_xk, target]) ;
This time, it shows
??? Undefined function or method 'feedforwardnet' for input arguments of type 'double'.
But how? the neural network runs like a charm with the default dataset
load house_dataset ;
which is also a 13 x 251 arrays both its input and output. How come it can't work with my cell array or numeric matrix? I've searched through the internet and no much literature covers this topic, and if it does, the discussion is still unclear. Thanks for the help in advance!
The question is quite simple but I can't find the answer.
I'm using j2me.
I have an integer array of 9 elements.
After all the computations now I want to print it or show it in a form.
It is something like Integer.toString(); for use with an array?
I know I can use a loop but I want to know if it's a faster way.
Use a loop but don't use the String + operator. Use StringBuffer.append().