Matlab - Vectorizing some dimensions of array? [closed] - arrays

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
I try vectorizing a 'for loop' for array but it does not work to me.
My 'for loop' is
for k = 1:N
R(n,n,k) = R(n,n,k) - SE3(k,k);
end
and vectorize it
diagSE3 = diag(SE3);
R(n,n,1:N) = R(n,n,1:N) - diagSE3(1:N);

With
R(n,n,1:N) = R(n,n,1:N) - diagSE3(1:N)
you try to subtract a vector (N x 1) from a 3-dimensional array with dimensions 1 x 1 x N. This can be repaired using squeeze:
R(n,n,1:N) = squeeze(R(n,n,1:N)) - diagSE3(1:N)

Related

Ruby, how to swap the elements in an array? [closed]

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 10 months ago.
Improve this question
How do I swap an(example) array's second and third elements? I tried the following
def swap_elements(array)
array = ["blake", "ashley", "scott"]
array[1], array[2] = array[2], array[1]
end
but I get
: ["scott", "ashley"]
I lost the first[0] element
There are many ways to do this - this is a functional approach that doesn't mutate the original array:
def swap_elements(array)
# yields the array to the block
array.then do |first, *rest| # deconstruct the array
rest.reverse # swap the places of 2 & 3
.unshift(first) # put the first back in
end
end

MATLAB Basics - interpreting parentheses and colons in array manipulation [closed]

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

Extract and assign selected rows of structure [closed]

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 4 years ago.
Improve this question
S.a=rand(100,3)
S.b=rand(100,3)
S.c=rand(100,3)
S.d=rand(100,3)
K.a=[ ]
K.b=[ ]
K.c=[ ]
K.d=[ ]
for i=1:numel(S)
if rand<0.8 % condition
K(i,:)=S(i,:) % How this assignment can be made
end
end
How to extract the rows of all the fields of a structure and store it to another structure.
I'm not entirely sure from you explanation if this is what you want. The following code will copy the fields from in S into K for just the rows where the items are < 0.8
S.a=rand(100,3);
S.b=rand(100,3);
S.c=rand(100,3);
S.d=rand(100,3);
K = [];
for field = ['a', 'b', 'c', 'd']
I = rand(length(S.(field)), 1) < 0.8;
K.(field) = S.(field)(I,:);
end

What is the logic to find out Leonardo number in C programming? [closed]

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 5 years ago.
Improve this question
Leonardo number
L(x)= {
1 if x=0
1 if x=1
L(x-1)+L(x-2)+1 if x>1
}
If x>1, what logic can I apply to find out the Leonardo number?
In your function L(int x)
If x is 0 or 1
return 1;
If x is greater than 1
return L(x-1) + L(x-2) + 1;
What I am doing using L(x - 1) is calling the function for the value x - 1 and the value will be evaluated directly as 1 if it is 0 or 1, otherwise it call again for (x - 1) - 1 i.e. x - 2 and the process repeats.
Same is the case for calling L(x - 2).
This is the logic, without the entire code.
For more information on this kind of logic, try searching for Recursion.

Ruby : How to compare 2 arrays of string [closed]

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

Resources