Create new array from integer elements of another array? MATLAB - arrays

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.

Related

Loadrunner : use parameter from list as array element number to get random array value

I want to use a random number from a list to select the particular element of a parameter array and use it elsewhere in the script as a parameter e.g.
Array is sspaidlist
My random integer from a parameter list is {GenRandomSSPAID}, which i want to use as the element of the sspaidlist array and save to is RandomSSPAID
lr_save_string(lr_eval_string("sspaidlist_{GenRandomSSPAID}"),"RandomSSPAID");
This just gets me the actual value "sspaidlist" and not the array.
I've also tried
sprintf(RandomSSPAID, "{sspaidlist_%d}", lr_eval_string("{GenRandomSSPAID}"));
but this seems to set RandomSSPAID to 0
The idea is to get 3 unique values - so 3 different array elements, I can't get the same value twice. I've offloaded the randomness to the loadrunner parameter functions, so I will always get a unique number with {GenRandomSSPAID}.
First convert your "GenRandomSSPAID" to integer as below:
i = atoi(lr_eval_string("{GenRandomSSPAID}"));
Now use sprintf to save it into RandomSSPAID as below:
sprintf(RandomSSPAID, "{sspaidlist_%d}", i);
You should be able to see value now.
I resolved this with the following code:
//declare c variables
Add_List()
{
....
char *RandomSSPAID;
char *SecondRandomSSPAID;
char *ThirdRandomSSPAID;
RandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(RandomSSPAID),"RandomSSPAID");
SecondRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(SecondRandomSSPAID),"SecondRandomSSPAID");
ThirdRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(ThirdRandomSSPAID),"ThirdRandomSSPAID");
lr_error_message("Random Values for iteration %s are : %s_%s_%s",lr_eval_string("{IterationNumber}"),lr_eval_string("{RandomSSPAID}"),lr_eval_string("{SecondRandomSSPAID}"),lr_eval_string("{ThirdRandomSSPAID}"));
....
}
Note that I offloaded the randomness to Loadrunner to generate a random number with {GenRandomSSPAID}, which is a parameter type of File, with a list of numbers and setting to select new row 'Random', Update value on 'Each occurance'

Concise way to create an array filled within a range in Matlab

I need to create an array filled within a range in Matlab
e.g.
from=2
to=6
increment=1
result
[2,3,4,5,6]
e.g.
from=15
to=25
increment=2
result
[15,17,19,21,23,25]
Obviously I can create a loop to perform this action from scratch but I wondering if there is a coincise and efficent way to do this with built-in matlab commands since seems a very common operation
EDIT
If I use linspace the operation is weird since the spacing between the points is (x2-x1)/(n-1).
This can be handled simply by the : operator in the following notation
array = from:increment:to
Note that the increment defaults to 1 if written with only one colon seperator
array = from:to
Example
array1 = 2:6 %Produces [2,3,4,5,6]
array2 = 15:2:25 %Produces [15,17,19,21,23,25]

how to get array of properties from array of objects in matlab

I am using an array of objects in my program, and each object has several attributes. I want to be able to extract separate arrays from this array of objects, one array for each attribute.
here is a snippet of the relevant code:
dailyDataMatrix(m,n)=dailyData('',''); %creates an mxn array of dailyData objects
for i=1:m
for j=1:n
dailyDataMatrix(i,j)=dailyData(datainput1, datainput2)%dailyData constructor, sets attributes
end
end
dailyDataMatrix.attribute
But when I try to access a certain attribute as in the code above, say of type string, I get a strange result. Instead of getting an array of strings, I get something else. When I try to print it, rather than printing an array, it prints a series of individual values
ans = 'string1'
ans = 'string2'
...
When I try to call
size(dailyDataMatrix.attribute)
className = class(dailyDataMatrix.attribute)
I get
"error using size: too many input arguments" and
"error using class: The CLASS function must be called from a class constructor."
However, when I write this as
thing=dailyDataMatrix.attribute
className = class(thing)
size(thing)
I get the response
classname = 'double' and size = 1x1.
Why is this not returning an array the same size as dailyDataMatrix? And an aside question is why the two different ways of writing the code above give different results? and how can I get the result that I want?
Thanks,
Paul
You can capture all the outputs using a cell array or using square brackets if the types are same. For regular array when all values are of same type use,
thing=[dailyDataMatrix.attribute];
If the types are different you can use
thing = cell(1,N); % N is the number of elements in dailyDataMatrix
[thing{:}] = dailyDataMatrix.attribute;

Selecting elements form a structure array

I am representing polynomials p and q as a structure array in the form:
p=struct('exponent',{2,3},'coeff',{1,1})
q=struct('exponent',{1,5,6,7},'coeff',{1,1,1,1})
I need to be able to select all the exponent elements of q for use in a function. I have attempted to do this by
a=q(1,(:)).exponent
I want a to become the array [1,5,6,7]. Thank you for any help in advance.
It's as simple as:
a = [q.exponent];
Accessing a field of a struct array like that returns a list of the values for each element in the array, which you can just catch in a concatenation operator.

How to copy consecutive values to an array

copyto = zeros(10)
what = ones(3)
where = 2
copyto[where:len(what)+where] = what
Is there a way to copy all values from a smaller array into a bigger array at a specific position, without providing the upper index? The way I thought it would work was
copyto[where:] = what
but this gives me
ValueError: operands could not be broadcast together with shapes
Thanks!
At the left and the right hand of the assignment you must have arrays with the same shape so that a one-to-one correspondence between the individual elements exist. In your case the array(view) copyto[where:] has 8 elements, while what has 3, so your assignment is not well defined. (Or to put it otherwise: there is no unique way to assign three values to eight variables, therefore the assignment is ill defined.)

Resources