Matlab: How to do `repelem` where each element is a matrix? - arrays

Suppose I have m-by-n matrices A, B, C arranged in an m-by-n-by-3 tensor P:
P = cat(3, A, B, C);
I now want to make a new tensor where each matrix is repeated K times, making the third dimension size 3K. That is, if K=2 then I want to build the tensor
Q = cat(3, A, A, B, B, C, C);
Is there a nice builtin way to achieve this, or do I need to write a loop for it? Preferably as fast or faster than the manual way.
If A, B, C were scalars I could have used repelem, but it does not work the way I want for matrices. repmat can be used to build
cat(3, A, B, C, A, B, C)
but that is not what I am after either.

As noted by #Cris Luengo, repelem(P, 1, 1, k) will actually do what you want (in spite of what the MATLAB documentation says), but I can think of two other ways to achieve this.
First, you could use repmat to duplicate the tensor k times in the second dimension and then reshape:
Q = reshape(repmat(P, 1, k, 1), m, n, []);
Second, you could use repelem to give you the indices of the third dimension to construct Q from:
Q = P(:, :, repelem(1:size(P,3), k));

Related

Does Kotlin support string/array slices by reference like Rust and Swift?

In Rust and Swift you can create a slice of either/both arrays and strings which doesn't make a new copy of the elements but gives you a view into the existing elements using a range or iterator and implemented internally a reference pair or reference + length.
Does Kotlin have a similar concept? It seems to have a similar concept for lists.
It's hard to Google since there is a function called "slice" which seems to copy the elements. Have I got it all wrong or is it missing?
(I'm aware I can work around it easily. I have no Java background btw.)
I don't think so.
You can use asList to get a view of the Array as a list. Then the function you already found, subList works as usual. However, I'm not aware of an equivalent of subList for Arrays. The list returned from asList is immutable, so you cannot use it to modify the array. If you attempt to make the list mutable via toMutableList, it will just make a new copy.
fun main() {
val alphabet = CharArray(26) { 'a' + it }
println(alphabet.joinToString(", "))
val listView = alphabet.asList().subList(10, 15)
println(listView)
for (i in alphabet.indices) alphabet[i] = alphabet[i].toUpperCase()
println(alphabet.joinToString(", "))
// listView is also updated
println(listView)
}
Output:
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
[k, l, m, n, o]
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
[K, L, M, N, O]
Using lists (or Collections) is preferred in Kotlin, and comes with much better support for generics and immutability than arrays.
If you need to mutate arrays, you probably have to manage it yourself, passing around (a reference to) the array and an IntRange of the indices you care about.

How to unpack/destructure an array in D?

How to unpack/destructure an array in D?
I have an array ([3,4,5]) of three elements and want to assign it to three variables (a, b, c) with one assignment.
How would I do this in D?
Try tie from my dub package vest:
import vest.utils: tie;
int a,b,c;
tie(a,b,c) = [1, 2, 2];
tie supports arrays, ranges, tuples
The module letassign.d at https://bitbucket.org/infognition/dstuff/src allows the following code:
int x, y, z;
let (x,y,z) = [1,2,3];
This should be in the standard D library, by the way!

Processing: How can I make a game board with an array?

I need to make a game board - using an array - for a Bomberman game remake in Processing.
This means the game board / array (which is rather large [9x9] and has 3 values [a,b,c] throughout), has to be able to:
Define the color/sprite of the according fields
Set the limit for where the character can walk
Have unique properties for each value/field (one value represents the
open space where the player can move around, another is a solid
block, and another one breaks when a bomb is blown up next to it and
transforms into an open field type)
I'm pretty much a programming noob and I barely a clue how to make all of this work..
I've already set up the array though which look like this:
int [][] board = {
{b, b, b, b, b, b, b, b, b},
{b, a, b, a, b, a, b, c, b},
{b, c, c, a, a, a, a, a, b},
{b, c, a, a, c, a, a, a, b},
{b, c, c, b, a, b, c, a, b},
{b, a, c, a, a, a, a, a, b},
{b, b, a, b, c, b, b, c, b},
{b, a, a, a, c, a, a, c, b},
{b, b, b, b, b, b, b, b, b} };
And I have managed to draw it as a monochrome chessboard.
Now I just have to figure out how to give each value the properties of the according block type.
Thanks for any help in advance :)
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:
You need to break your problem down into smaller pieces and then approach each of those pieces one at a time.
For example, I'd start with a simple program that just draws a grid of squares. Then work your way up from there: can you make it so the number of squares is a variable (or two variables) that you define at the top of your sketch? Now make it so each square is a different color. Now make it so each square is reading from the array.
Work your way up in small pieces, and if you get stuck, post a MCVE in a new question and we'll go from there. Good luck.
Going off of what Kevin Workman said, here is code you could possibly use for a grid of squares:
void setup(){
size(800, 800);
background(255, 204, 5);
fill(158, 10, 10);
for(int i = width/8; i <= width; i += width/4){
for(int v = height/8; v <= height; v += height/4){
rect(i, v - height/8, width/8, height/8);
rect(i - width/8, v, width/8, height/8);
}
}
}
It's not necessarily an array, but a for loop does the job.

Add an object to an Array based on dictionary Array

I believe this would be an algorithm question, so it doesn't matter what language this is in, although I think in my case it will be implemented in jquery.
So the idea is I have two arrays of type Object. In this object there are many variables, but the only relevant one to this is a String. Lets say for argument sake, I have array [A, A, A, A, B, B, E, E, F] and the 'dictionary' array [A, B, C, D, E, F]. Whenever I want to add something to the first array, I want it to be added by the location of the object in the second array. So for instance, if I wanted to add D, the first array would now look like [A, A, A, A, B, B, D, E, E, F].
To add D, there is not necessarily a C or D to search for to put the D in after. In addition, there can be repeats like in the example. The array could also be empty that the program is adding 'D' to. Also, if there are already Ds in the first array, the D would go after the last D.
Important: The letters I used are only for clarification sake, the actual code uses words that are NOT in alphabetical order, so solutions using alphabetical order do NOT work in this instance.
Is there a better solution to this then brute force?

Is it possible to concatenate 3D matrices in Matlab?

Lets say I have two matrices A and B that are 3D. A is 420x420x3 and B is 420x420x3. After concatenation I want to obtain C that is 420x420x6 as the concatenation of A and B in the third dimension.
How would I do that at matlab ?
Simply use cat:
C = cat(3, A, B);
Best,

Resources