VBA compare Abs() values not working - arrays

I have two arrays of data where I want to compare every element in both arrays and put the biggest absolute value in an new array. See code below:
If Abs(DataArray1(i)) < Abs(DataArray2(i)) Then
NewDataArray(i) = DataArray2(i)
Else
NewDataArray(i) = DataMinArray(i)
End If
The probem is that some of the values are decimals and if I compare for example -1.02 and 1.0100 then normaly -1.02 has bigger abs value. In my case my program returns 1.0100 and it happens to all values are not the same length. The longer one is always returned. How could that happen and what is wrong?

are the arrays of String type? Otherwise i couldn't explain why values like 1.0100 even exist. That value should automatically be shown as 1.01
If you are working with Strings then try to convert the values to floats. This could work: Abs(CDbl(DataArray1(i))) < Abs(CDbl(DataArray2(i)))

Related

How to parse through array of objects and convert string numbers to integers

Working with JS. I have an array of objects. Here is a sample below. You can see that all the values are strings. However, I need to convert the lft and rgt values into integers so that I can compare these values against other values held in a different array. Keeping them as strings and comparing gives me erroneous results.
nodesWithchildren
:Array[1]
0:Object
category_id:"8"
created_at:"2016-11-04 14:14:28"
lft:"17"
name:"Felt Materials"
rgt:"22"
updated_at:"-0001-11-30 0:00:00"
__proto__:Object
I have searched through Stackoverflow and found various suggestions. All of them use for loops with parseInt which is great BUT they assume that the values either are all string numbers AND they assume that the values are held in a simple array. Not an array of objects.
I tried using this, but this does NOT work. Probably because parseint does not like it. IN fact it crashes the page:
var nodesWithchildrenParsed =[];
for(var i=0; nodesWithchildren.length;i++){
nodesWithchildrenParsed[i] = parseInt(nodesWithchildren[i]);}
My first step is converting these values. Once I have the lft and rgt converted to integers I will run another function where I will say something like, "if lft is greater than X and rgt is less than Y then give me the name "felt materials".
Tks !
The easy way to convert a string to a number without using parseint() is to simply divide the string by the integer 1. In my case this was a perfect solution as I did not have to deal with the fact that the strings were in an array of objects, other that doing this:
var childNodes=[];
for(i=0; i < selectedArray.length; i++){
if((selectedArray[i].lft /1 ) > (nodesWithchildren[0].lft /1) && (selectedArray[i].rgt/ 1) < (nodesWithchildren[0].rgt/1)) {
childNodes += selectedArray[i].name;
}
}
JS will convert the string before undertaking any operations on it. Cody's Example:
console.log( ('2'/1) > ('10'/1) );
Credit goes to Cody and his answer here:
Cody's answer

matlab cell contents , how to tell what they are?

I have a cell array, c, filled with hexadecimal data and when I view the cell contents by typing c at the matlab prompt, it shows me contents enclosed between ticks, i.e., '0x0009'. But, one element is enclosed in brackets and looks like [650345]. How can I convert the [ ] data to ' ' data? When I do iscellstr on this particular element, matlab returns 0. iscellstr returns 1 for all other elements of c.
I'm reading this data into matlab from excel and I fear that excel 'helped' me by converting one hex value to scientific notation. I can't, as far as I've found, change what excel did. I think the true value is lost and unrecoverable. But I need to convert this one outstanding value, even if incorrect, to be like the other cell values so that I can carry on with my processing. Any suggestions?
If you know the index of wrong value and it's true value, you just do:
c(idx) = {'0x0009'};
I think this does what you want:
ind = cellfun(#isnumeric, c); %// find numeric cells
c(ind) = cellfun(#(s) ['0x' dec2hex(s)], c(ind), 'uniformout', 0); %// convert to
%// hex string and prepend '0x'
Example: input
c = {'0x0009', 650345};
produces the output
c =
'0x0009' '0x9EC69'

Create new array from integer elements of another array? MATLAB

I have created an array tP which contains a mix of integer and non-integer elements. I want to create a new array of the integer element.
The result I would like is in the same form as is returned for, for example:
tP2=find(tP>300);
That is, a list of the element numbers which contain integer values, not a list of the integers themselves.
From this I will then select the desired elements like so:
tP3=tP(tP2);
To do this for integers, what I currently have is:
tP2=find(isinteger(int16(tP)));
But instead of a list of element numbers, I just get tP2=1 returned.
Why does isinteger not work in this case and how can I achieve my required result? Thanks.
use round
tp2 = find( tP == round(tP) );
As Shai says, comparison to round is an effective way to detect integers.
Next, unless you need the list of matches for something else, you don't need find. Just the comparison will create a mask array, and masks can be used for subscripting.
tP3 = tP(tP == round(tP));
Getting rid of tP2 and the call to find should save time and memory.

Sum along absolute values in an Array in Matlab

My array contains a string in the first row
how can I sum the array from the 2nd row to the Nth/1442th row (as in my example) disregarding the negative signs present in the column?
for example, my code for an array called data2 is:
S = sum(data2(2,15):data2(1442,15));
so sum all of the elements from row 2 to row 1442 in column 15.
This doesn't work but it also does not have anything to deal with the absolute value of whatever row its checking
data is from a .csv:
You should do something like this:
sum(abs(data(2:1442,15)));
The abs function will find the absolute value of each value in the array (i.e. disregard the negative sign). data(2:1442,15) will grab rows 2-1442 of the 15th column, as you wanted.
EDIT: apparently data is a cell array, so you could do the following, I think:
sum(abs([data{2:1442,15}]));
Ok so it looks like you have a constant column so
data2(2,15) = -0.02
and further down
data2(1442,15) = -0.02 %(I would assume)
So when you form:
data2(2,15):data2(1442,15)
this is essential like trying to create an array but of a single value since:
-0.02:-0.02
ans =
-0.0200
which of course gives:
>> sum(-0.02:-0.02)
ans =
-0.0200
What you want should be more like:
sum(data2(2:1442,15))
That way, the index: 2:1442, forms a vector of all the row references for you.
To disregard the negative values:
your answer = sum(abs(data2(2:1442,15)))
EDIT: For a cell array this works:
sum(abs(cell2mat(data2(2:1442,15))))

Finding whether a value is equal to the value of any array element in MATLAB

Can anyone tell me if there is a way (in MATLAB) to check whether a certain value is equal to any of the values stored within another array?
The way I intend to use it is to check whether an element index in one matrix is equal to the values stored in another array (where the stored values are the indices of the elements which meet a certain criteria).
So, if the indices of the elements which meet the criteria are stored in the matrix below:
criteriacheck = [3 5 6 8 20];
Going through the main array (called array) and checking if the index matches:
for i = 1:numel(array)
if i == 'Any value stored in criteriacheck'
%# "Do this"
end
end
Does anyone have an idea of how I might go about this?
The excellent answer previously given by #woodchips applies here as well:
Many ways to do this. ismember is the first that comes to mind, since it is a set membership action you wish to take. Thus
X = primes(20);
ismember([15 17],X)
ans =
0 1
Since 15 is not prime, but 17 is, ismember has done its job well here.
Of course, find (or any) will also work. But these are not vectorized in the sense that ismember was. We can test to see if 15 is in the set represented by X, but to test both of those numbers will take a loop, or successive tests.
~isempty(find(X == 15))
~isempty(find(X == 17))
or,
any(X == 15)
any(X == 17)
Finally, I would point out that tests for exact values are dangerous if the numbers may be true floats. Tests against integer values as I have shown are easy. But tests against floating point numbers should usually employ a tolerance.
tol = 10*eps;
any(abs(X - 3.1415926535897932384) <= tol)
you could use the find command
if (~isempty(find(criteriacheck == i)))
% do something
end
Note: Although this answer doesn't address the question in the title, it does address a more fundamental issue with how you are designing your for loop (the solution of which negates having to do what you are asking in the title). ;)
Based on the for loop you've written, your array criteriacheck appears to be a set of indices into array, and for each of these indexed elements you want to do some computation. If this is so, here's an alternative way for you to design your for loop:
for i = criteriacheck
%# Do something with array(i)
end
This will loop over all the values in criteriacheck, setting i to each subsequent value (i.e. 3, 5, 6, 8, and 20 in your example). This is more compact and efficient than looping over each element of array and checking if the index is in criteriacheck.
NOTE: As Jonas points out, you want to make sure criteriacheck is a row vector for the for loop to function properly. You can form any matrix into a row vector by following it with the (:)' syntax, which reshapes it into a column vector and then transposes it into a row vector:
for i = criteriacheck(:)'
...
The original question "Can anyone tell me if there is a way (in MATLAB) to check whether a certain value is equal to any of the values stored within another array?" can be solved without any loop.
Just use the setdiff function.
I think the INTERSECT function is what you are looking for.
C = intersect(A,B) returns the values common to both A and B. The
values of C are in sorted order.
http://www.mathworks.de/de/help/matlab/ref/intersect.html
The question if i == 'Any value stored in criteriacheck can also be answered this way if you consider i a trivial matrix. However, you are proably better off with any(i==criteriacheck)

Resources