How to convert cell array of string numbers into numerical vector [duplicate] - arrays

This question already has answers here:
Convert cell to array in matlab
(3 answers)
Closed 6 years ago.
I am using Matlab and I have the following data:
a = {'3' '11' '7'}; % This is what I have.
My goal is to use a function to convert such cell into a vector having the following shape:
b = [3 11 7]; % What I need to get!
If I try to use the b = cell2mat(a) function the result that I get is this number: b = 3117; which is not what I want.
What function should I use to reach my goal? If possible please do not use for loops but just Matlab functions.

You're looking for str2double:
b = str2double(a);

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

Is there a quick way concat the values returned by Julia's digits() method back into it's original number? [duplicate]

This question already has answers here:
Get a number from an array of digits
(3 answers)
Closed 3 years ago.
I really like Julia's Digits function which will return an array of the individual digits that make up the input integer (See code example). My question is, once you have this array, is there an easy way to join the individual values back into the original value passed into digits?
Now, I suppose I could just take each integer's index value and multiply by it's corresponding power of 10 and modify the array in place. Then I could use sum on the array, but is there a better way to do it?
I.E
function getDigits(x)
return digits(x)
end
Julia> getDigits(1234)
4-element Array{Int64,1}:
4
3
2
1
function joinDigits(digitArray)
for i in 0:length(digitArray)-1
digitArray[i+1] = digitArray[i+1] * 10 ^ i
end
return sum(digitArray)
Julia> joinDigits([4,3,2,1])
1234
foldr((rest, msd) -> rest + 10*msd,x) is a one liner that does the same thing. It's terse, but probably not as clean as the for loop.

how to access an element of an n-D matrix where index comes from a mathematical operation [duplicate]

This question already has answers here:
MATLAB: Accessing an element of a multidimensional array with a list
(2 answers)
Use a vector as an index to a matrix
(3 answers)
Closed 5 years ago.
How can I access an element of an n-D matrix where index comes from a mathematical operation in Matlab?
For example I have a 4D Matrix called A.
I want to access element 1,1,1,1 which results from (3,4,5,6) - (2,3,4,5)
Is there any way I can do this assuming that the array can be any dimension d and that the array from subtraction will always be d elements long?
One possible way would be to utilise the fact that MATLAB can use linear indexing for any n-dimensional array as well as row-column type indexing. Then you just have to calculate the linear index of your operation result.
There may be a more elegant way to do this but if x is the array holding the result of your operation, then the following works
element = A(sum((x-1).*(size(A).^[0:length(size(A))-1]))+1);
The sub2ind function feels like it should help here, but doesn't seem to.
Another approach is to converting to a cell array, then to a comma-separated list:
A = rand(3,4,5,6); % example A
t = [2 1 3 4]; % example index
u = num2cell(t);
result = A(u{:});

Matlab: convert (i,j) matrix to (1,j) array [duplicate]

This question already has answers here:
the easiest way to convert matrix to one row vector [duplicate]
(2 answers)
How do you concatenate the rows of a matrix into a vector?
(2 answers)
Closed 5 years ago.
I got a 365x24 matrix (hours of the day x days of the year) and I'd like to convert it to a 1x8760 matrix (for all the hours of the year).
So basically, every row of the original matrix should be copy pasted after the previous row.
How can this be done?
Thanks!
For any matrix, the (:) indexing operation concatenates the columns of the matrix to form a vector.
>> a = [1,2,3; 4,5,6];
>> a = a(:)
ans =
1
4
2
5
3
6
In your case, you want the rows concatenated. To achieve this, simply transpose the matrix before indexing with (:). Finally, you can simply transpose it to get the row vector.

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?

Resources