Let's say, that i have a cell array of cells containing only numeric values. Its name is Q.
Q = { {[0] [1] [0] [238]} }
Q{1}
ans =
[0] [1] [0] [238]
What i would like to do, is to combine all these 4 cells into just one.
In that case, it would be something like the one below:
Q{1}
ans =
0 1 0 238
Any help would be really appreciated.
You have a double-nested cell array:
Q = { {[0] [1] [0] [238]} }
and you need comma-separated lists to transform it into an array. I assume you have multiple cell arrays within Q, so you can use cellfun:
out = cellfun(#(x) [x{:}], Q,'uni',0)
and you get
Q{1} =
[0] [1] [0] [238]
out{1} =
0 1 0 238
For one element this is equivalent to:
Q{1} = [Q{1}{:}]
as the x in the cellfun operation is equivalent to Q{i} where i is the running variable.
But if you you just have this one array in your cell array, consider:
out = cell2mat(Q{:})
as you don't need it to be a cell array at all.
Try doing this:
Q{1} = [Q{1}{:}]
Related
Two questions, one fairly simple question (at least it seems it should be simple) and one that may take a bit more work. Feel free to contribute to either or both.
First, I'd like to create a string array based off of an existing string array based on a criteria. Take for example a similar operation with a double array:
>> nums = [ 1 2 1 2]
nums =
1 2 1 2
>> big_nums = (nums == 2) .* nums
big_nums =
0 2 0 2
I'd like to do something similar with a string array, however I don't know what function to use:
>> sizes = ["XL" "L" "XL" "L"]
sizes =
1×4 string array
"XL" "L" "XL" "L"
>> large_sizes = (sizes == "L") .* sizes
Undefined operator '.*' for input arguments of type 'string'.
I'd like the output to be
large_sizes =
1×4 string array
"" "L" "" "L"
Second question. Suppose I have a 2 dimensional cell array. I'd like to filter data based on criteria:
>> data = {"winter", 1; "spring", 2; "summer", 3; "fall", 4}
data =
4×2 cell array
["winter"] [1]
["spring"] [2]
["summer"] [3]
["fall" ] [4]
>> nice_weather = ( (data(1,:) == "fall") + (data(1,:) == "spring") ) .* data
Error using ==
Cell must be a cell array of character vectors.
I'd like a code that results in one of two arrays:
nice_weather =
4×2 cell array
[""] [1]
["spring"] [2]
[""] [3]
["fall"] [4]
----- OR -----
nice_weather =
2×2 cell array
["spring"] [2]
["fall"] [4]
For this question, I am also open to separating data into multiple arrays (for example, one array for strings and one array for numbers).
Thanks!
This solution uses the strcmpi function from MATLAB (no toolbox required) to compare two strings (insensitive to the case).
1D Cell Array:
sizes = {'XL' 'L' 'XL' 'L'}; % Changed " to ' & used cell array
idx = strcmpi(sizes,'L'); % Logical index
sizelist = {sizes{idx}}
Or you could try something like
sizes(~idx) = {"" ""} % manual just for example
For this to automatically adjust the number of blanks "", you could use repmat like this
sizes(~idx) = repmat({""},1,sum(~idx))
Output:
sizes = 1×4 cell array
{[""]} {'L'} {[""]} {'L'}
2D Cell Array:
data = {'winter', 1; 'spring', 2; 'summer', 3; 'fall', 4}; % Changed " to '
nicemo1 = 'spring';
nicemo2 = 'fall';
idx = strcmpi(data(:,1),nicemo1) | strcmp(data(:,1),nicemo2); % Obtain logical index
data(idx,:)
Output:
ans = 2×2 cell array
{'spring'} {[2]}
{'fall' } {[4]}
Tested with MATLAB R2018b.
Also beware variables like sizes as dropping a letter masks a useful function, size.
I have n number of arrays and I want to work out if there is a common value in these arrays. If I knew the number of arrays I could do something like:
a = [1,2,3]
b = [2,4,5]
c = [2,6,7]
x = a & b & c
x == [2]
However, this isn't possible if you don't know the number of arrays. So far I've come up with this:
array_of_integers = [[1,2,3],[2,4,5]....]
values = []
array_of_integers.each_with_index do |array, index|
values = if index.zero?
array
else
values & array
end
end
# `values` will be an array of common values
However, this doesn't seem very efficient. Is there a better way?
However, this isn't possible if you don't know the number of arrays.
Actually, Enumerable#reduce can help with it:
[[1,2,3], [2,4,5], [2,6,7]].reduce(&:&) # => [2]
&:& looks interesting, but it's just:
[[1,2,3], [2,4,5], [2,6,7]].reduce { |memo, el| memo & el } # => [2]
Or it's also possible to do it as #Jagdeep suggested:
[[1,2,3], [2,4,5], [2,6,7]].reduce(:&) # => [2]
I have multiple array, number can be arbitrary. but the size of all array is same. How do i add each element of with respective element of all the arrays and maybe save it in another array
A1 = [1 2 3 4 5 6]
A2 = [1 2 3 4 5 6]
.
.
.
.
final = [1+1+1+... 2+2+2+.... 3+3+3+3.... 4+4+4.... 5+5+5+5... 6+6+6+6...]
As your arrays are all the same length you can just add the arrays forming a new array.
final = A1+A2
This function searches in your workspace looking for all variables containing capital 'A'. The for loop adds all found variables. If there are other variables containing 'A', other restrictions has to be made.
variables = who %# all variable names from workspace
index = strmatch('A',variables) %# indices matching "A"
newarray = 0
for j = 1:numel(index)
tmp = eval(char(variables(index(j)))); %# store variable in tmp
newarray = newarray + tmp; %# sum
end
If you have an unknown number of A's, you can try something like this:
final = 0
i = 1
while exist(['A' num2str(i)]) == 1 % ['A' num2str(i)] constructs the variable name, eval calls it
final = final + eval(['A' num2str(i)]);
i = i + 1;
end
This should work as long as the variables are stored in the workspace, are of the same length and are named A1, A2, A3, ... A9, A10, ...
Let's say you have this structure (as you write in the comments):
main = struct('err',{1:6,5:10,1:6,1:6},'seg_err',{1:6,5:10,1:6,5:10});
you can convert it to matrix:
m = vertcat(main.seg_err);;
And than take the sum in a simple command:
final = sum(m)
which results:
final =
12 16 20 24 28 32
and thanks to #beaker :)
im tryin to sort a two-dimensional Array in Ruby by the first value, like that:
files_array = Array.new(2) {Array.new}
files_array[0][0] = 42
files_array[1][0] = "/media/js/aefc015sdfsdf0728175535.js42"
files_array[0][1] = 21
files_array[1][1] = "/media/js/aefc015sdfsdf0728175535.js21"
files_array[0][2] = 30
files_array[1][2] = "/media/js/aefc015sdfsdf0728175535.js30"
i tried
files_array.sort!{|a,b| b[0] <=> a[0]}
but it returns:
`sort!': comparison of Array with Array failed (ArgumentError)
This is how i want the array to be sorted:
files_array[0][0] = 21
files_array[1][0] = "/media/js/aefc015sdfsdf0728175535.js21"
files_array[0][1] = 30
files_array[1][1] = "/media/js/aefc015sdfsdf0728175535.js30"
files_array[0][2] = 42
files_array[1][2] = "/media/js/aefc015sdfsdf0728175535.js42"
`sort!': comparison of Array with Array failed (ArgumentError)
This is because a[0] and b[0] are elements of an array of arrays, i.e. they are arrays themselves.
If you did this:
files_array.sort!{|a,b| b[0][0] <=> a[0][0]}
It would work.
Incidentally, this looks like a great example of when an associative array would be a better idea than an array of arrays.
files_hash = {
42 => "/media/js/aefc015sdfsdf0728175535.js42",
21 => "/media/js/aefc015sdfsdf0728175535.js21",
30 => "/media/js/aefc015sdfsdf0728175535.js30"
}
Not only is this a lot clearer in code, but you do away with the need to maintain a sorted 2-dimensional array.
try this it will work for you:
files_array = [[42,"/media/js/aefc015sdfsdf0728175535.js42"],[21,"/media/js/aefc015sdfsdf0728175535.js21"],[30,"/media/js/aefc015sdfsdf0728175535.js30"]]
sorted = files_array.sort { |x,y| x[0] <=> y[0] }
puts sorted
Result:
21
/media/js/aefc015sdfsdf0728175535.js21
30
/media/js/aefc015sdfsdf0728175535.js30
42
/media/js/aefc015sdfsdf0728175535.js42
I have cell array like A = {1;2;3;4;5}
I want to remove the row where value in the cell is equal to 4.
I tried
b = A{A~=3}; but it does not work with cell array.
This will do:
A = {1;2;3;4;5}
A([A{:}] == 4) = []
A =
[1]
[2]
[3]
[5]