Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 2 cells functions (resulted from certain code) as follows:
p =
'GO:0008150'
'GO:0016740'
'GO:0016787'
'GO:0008150'
'GO:0016740'
'GO:0016740'
'GO:0016787'
'GO:0016787'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0008150'
'GO:0008150'
'GO:0006810'
'GO:0016192'
'GO:0006810'
'GO:0005215'
c =
'GO:0016740'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0016779'
'GO:0004386'
'GO:0003774'
'GO:0016298'
'GO:0016192'
'GO:0005215'
'GO:0030533'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0003774'
'GO:0005215'
'GO:0030533'
I have a code that it works fine with single values (numericals or characters), but it doesn't work with the above cellsarrays, and the following error message appears:
??? Undefined function or method 'eq' for input arguments of type 'cell'.
If there any idea about how to convert from cells function to any kind which can accept the following code:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if a(1,:)==level1_root
level=a(2);
level=[level a(2)]
else
end
end
In your code, a is a cell array of two strings, while level1_root is a cell array with one string. You can not compare cell arrays of strings using ==. You should rather to a strcmp. For example, something like this:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if strcmp(a{1}, level1_root{:})
level=a(2);
level=[level a(2)]
else
end
end
However, I am not sure if the above code will work for you as it is. It assumes that level1_root has 1 element only. That is the case in the example arrays, but may not be in general. You will have to change the code to suit your needs.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
enter image description here
k=15;
for i=1:k:length(aa) % aa = 100
for j=1:k:length(bb) % bb = 200
result(j,i) = function(a, b, c, d)
end
end
I am trying to save the calculated value by extracting k by 15 steps.
If you do it this way, the next value will come out from the 1st row, 1st column, 16th row, 1st column, as shown in the picture above, and the value of 0 will come out in between.
So, I'm trying to write additional code that pulls them out in sequence like the right part of the picture...
for ii = size_aa:-1:1
if result(ii,:) == 0
result(ii,:) = [];
end
end
I tried the method of removing zero elements, but it takes too much time. Is there any other way?
Once you have your result matrix, you can use result(result == 0) = []; to remove zero elements and end up with a vector of non-zero results. Here's an example derived from the code the OP posted (I replaced function(a,b,c,d) with rand() for the sake of the example):
k=15;
for i=1:k:100
for j=1:k:200
result(j,i) = rand();
end
end
result(result == 0) = [];
Alternatively, the solution proposed by Cris Luengo generates a matrix that doesn't contain zero elements to start with; I plugged his idea in my sample code:
k=15;
for i=1:k:100
for j=1:k:200
result((j-1)/k+1, (i-1)/k+1) = rand();
end
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
What does it mean in MATLAB when I do:
array = array (1:number)
and what does it mean:
array = array(indexes_array)
and finally, what does it mean:
array = array(indexes_array,:)
Answers, according to MATLAB docs:
1)
array = array (1:number)
The colon notation in MATLAB provides an easy way to extract a range of elements from v:
v(3:7) % Extract the third through the seventh elements
ans =
9 4 2 11 7
2)
array = array(indexes_array)
array is reorganized according to indexes_array order, assuming that indexes_array is composed of indexes.
3)
array = array(indexes_array,:)
just the same as number 1), array is reorganized according to all the rows of indexes_array, ignoring the columns.
References:
https://www.mathworks.com/help/matlab/math/array-indexing.html
https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 2 arrays of strings as under. I want to compare them and execute some code if these arrays are not equal-
current_instances = ["170601_7711", "170601_8811"]
app_instances = ["170602_7711", "170602_8811"]
How can I compare them in ruby?
x = ["alpha1", "beta1"]
y = ["alpha2", "beta2"]
Check if all elements are the same
x == y #=> false
Compare each element
Compare each string at some index i, assuming both arrays are the same size. Then apply some code to the matches.
x.zip(y) #=> [["alpha1", "alpha2"], ["beta1", "beta2"]]
x.zip(y).map {|a,b| a == b ? 'do this' : 'else do this' }
#=> ["else do this", "else do this"]
Perhaps you can do an array difference with the - operator and execute your code if the difference is zero
arr_diff = current_instances - app_instances
This is the simplest solution I could think of :)
Check my solution and let me know how it goes
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What i want to do,is print random elements from array,heres example code:
myTable = { "one", "two", "three","four"}
-- print here: one,three,four
Printing random element is simple -- print(myTable[math.random(#myTable)]) -- but if you need to make each printed element unique, then you better shuffle the elements in the array and print the resulting elements one-by-one. You may check this SO answer for ideas.
If you want N amount of elements, you need to make use of a loop:
local myTable = { "one", "two", "three","four"}
local result = {}
for i=1,3 do -- N here, e.g 3 if you want 3 elements
result[i] = table.remove(myTable,math.random(#myTable))
end
print(table.concat(result,", "))
-- "four, two, three" as an example
The code will error if you request more elements than there are in the table. If you want to reuse the table later on, you'll have to copy it, as this code actually removes elements from the table.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say, we have the following two-dimensional array in Matlab:
A=[0 451
0 446
0 543
.....]
etc. I want to create another, one-dimensional array, that will do this:
For example, lets call the 1-D array B, B(1) will "show" to [0 451]. B(2) will "show" to [0 446], B(3) will "show" to [0 543] and so on.I hope that my desired result is pretty clear to anyone who could give me a bit help.
Two ways:
a=1:10
split_a1=(reshape(a,2,[])).';
Access split_a1 as split_a1(1,:),...,split_a1(5,:);.
split_a2=mat2cell(a,1,2*ones(1,numel(a)/2));
Access split_a2 as split_a2{1},...,split_a2{5};.
Well, what you have just set is impossible, you are mixing the arrays and the Dimensions.
As you have explained it, B is 2-D, and A is 1-D. You can do what you want by doing this:
j=0;
i=1;
while i<=size(A,2)/2;
j=j+1;
B(i,1)=A(j);
j=j+1;
B(i,2)=A(j);
i=i+1;
end