How to use a cell array directly returned from a function? [duplicate] - arrays

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I have a path (without a file name at the end) as a string in Matlab and i want to receive the first parent directory (the directory after the last file seperator character) in it.
At the moment i'm doing it like this:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = strsplit(filePath , filesep);
>>firstParent = firstParent{end};
>>disp(firstParent);
DOF
Is there any way i can use strsplit's return value (a cell array) without assinging it to a variable first?
Something like:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = ( strsplit(filePath , filesep) ){end};
>>disp(firstParent);
DOF

Do you mean:
[~,firstParent] = fileparts ( 'D:\TRAIN_DATA\OBSTACLES\DOF' )

Related

Check item contains in model object array in swift 3 [duplicate]

This question already has answers here:
how can I check if a structure is in the array of structures based on its field in Swift3?
(3 answers)
Get element from array of dictionaries according to key
(1 answer)
Closed 5 years ago.
I have a model object called user.
how to find my user.id from all users collection.
I need something like this UsersCollection.contains(user.id)
and returns Bool value.
Here you go.
let contains = UsersCollection.contains{ $0.id == 0 }
Edit:
let object = UsersCollection.first{ $0.id == 0 } // object is optional here.

How to create array and display required array value [duplicate]

This question already has answers here:
How does one declare an array in VBScript?
(2 answers)
Closed 5 years ago.
how to create array and display required array value. For example:
Arr[0]="anand" Arr[1]="bala".print Arr optin:2, Bala .
i need the required array output
Queat seems a bit unclear.
May be below piece of code can be relevent to your Question.
'Initializing & Filling up array
Dim testArr(3)
testArr(0)="First"
testArr(1)="Second"
testArr(2)="Third"
Dim Iterator,userInput
'If you Need ro validate the User input you need to use Err Object.
'Like if it's number or not/Is teh User Provided value is positive or not/ etc. etc. which i'm not doing now)
userInput=Cint(inputbox("Please enter a Positive number between 1-" & Ubound(testArr)))
If userInput<=Ubound(testArr) Then
MsgBox(testArr(userInput-1))
Else
MsgBox("Wrong Value Provided. Not Applicable Array Index Number !!!")
End If

MATLAB: cannot call or index into a temporary array [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I am trying to read data files each containing one column and 4097 rows. But my function needs total numbers of rows with even number (means 4096). So I used the MATLAB command x(2:length(x))). But my 'x' value in this command is a(:,k) and the issue is MATLAB cannot call or index into a temporary array. Any solution to this? I thank all for the support.
The code is:
for k = 1:9
with filename = sprintf('F00%d.txt',k);
a(:,k) = load(filename);
x = a(:,k)(2:length(a(:,k)));
w = tqwt(p,1,3,3);
[a1,a2,a3,a4]= deal(w{:});
m(a1,1) = mean(a1);
s(a1,1) = std(a1);
ma(a1,1) = max(a1);
mi(a1,1) = min(a1);
Unfortunately, you have to split x = a(:,k)(2:length(a(:,k))); into two lines as shown below:
temp = a(:,k);
x = temp(2:length(a(:,k)));
Please read:
Indexing of a function's return
How can I use indexing on the output of a function?

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

function to get a random element of an array bash [duplicate]

This question already has answers here:
How to pass array as an argument to a function in Bash
(8 answers)
Closed 8 years ago.
I know how to get a random item from an array like that:
declare -a ARRAYISO=(100 200 400 800)
echo ${ARRAYISO["$[RANDOM % ${#ARRAYISO[#]}]"]}
I could obviously do that for each array, like a donkey, but I'd like to make a function which takes an array as argument and returns a random element.
I'm trying with:
randArrayElement() {
randElement=${$1["$[RANDOM % ${#$1[#]}]"]}
echo $randElement
}
randArrayElement ARRAYISO
but it doesn't like my $1... I've tryed with ", ', ` , bash doesn't interpret the $1 var...
Change your function to:
randArrayElement(){ arr=("${!1}"); echo ${arr["$[RANDOM % ${#arr[#]}]"]}; }
and call it as:
randArrayElement "ARRAYISO[#]"

Resources