Modify Matrix / multidimensional array with Colon in Colon
22 views (last 30 days)
Show older comments
In Matlab I can modify an array with a colon like this:
n = 90;
A = zeros(n,1);
A(1:n) = (1:n).^2;
If I have a two dimensional array, a matrix it gets more complicated but it is still easy to modify all entities with a loop:
n2 = 10;
A = zeros(n,n2);
for index = 1:n2
A(1:n,index) = (index:index+n-1).';
end
or
n2 = 10;
A = zeros(n,n2);
for index = 1:n2
A(1:n,index) = colon(index, index+n-1).';
end
I would like to use a direkt matrix calculation instead of a for loop to increase my speed, since my problem is bigger and I have to do it often.
My approach was:
A(1:n,1:n2) = colon(1:n2, (1:n2)+n-1).';
or
A(1:n,1:n2) = colon(1:n, (1:n)+n2-1);
Matlab also uses "n" and "n2" on the left side, giving me the 90-by-10 size. However, on the right side I only have a 90-by-1 or 1-by-10 size.
My current solution is unfortunately not the colon. I need to use the arrayfun and convert the resulting cell array into a matrix.
A2 = arrayfun(@(a) a:a+n-1, 1:n2, 'UniformOutput', false).';
A = cell2mat(A2).';
In Python I would use nested foor loops and be able to make this multidimensional in a singel line.
My working alternative in Matlab is significantly more computationally expensive and for more dimensions I would have to create a loop or a function that calls itself.
Isn't that somehow easier with "colon"?
2 Comments
Stephen23
on 3 Aug 2023
Edited: Stephen23
on 3 Aug 2023
"Isn't that somehow easier with "colon""
I doubt it.
n = 90;
n2 = 10;
A2 = arrayfun(@(a) a:a+n-1, 1:n2, 'UniformOutput', false).';
A = cell2mat(A2).'
Two efficient MATLAB approaches:
B = (0:n2-1) + (1:n).'
C = hankel(1:n,n:n+n2-1)
isequal(A,B,C)
The trick to using MATLAB effectively is to not reinvent the wheel: for many common operations there are simple functions or operations that can be used on entire arrays:
"Isn't that somehow easier with "colon""
You seem to be trying to hide a loop inside COLON: it does not work like that.
Accepted Answer
recent works
on 4 Aug 2023
You are correct that using the colon operator directly to perform the multidimensional assignment is a bit tricky in MATLAB. The colon operator is designed for creating a linearly spaced vector and may not directly produce the desired multidimensional array.
However, you can achieve the desired result in a more efficient way using array broadcasting and bsxfun (Binary Singleton Expansion Function) in MATLAB. Array broadcasting allows you to perform element-wise operations on arrays with different sizes, expanding the smaller array to match the size of the larger one.
Here's how you can do it without using loops or cell arrays:
n = 90;
n2 = 10;
% Create a 1-by-n vector with values from 1 to n
index = 1:n;
% Create a 1-by-n2 vector with values from 1 to n2
index2 = (1:n2)';
% Perform element-wise addition and broadcasting to create the desired matrix
A = bsxfun(@plus, index2, index - 1);
In this approach, we create two vectors index and index2 using the colon operator. Then, we use bsxfun to add index2 to each element of index, which results in the desired multidimensional matrix A.
This method should be more computationally efficient and avoids the need for loops or cell arrays. It can be easily extended to higher dimensions as well.
2 Comments
Dyuman Joshi
on 6 Sep 2023
@Julian what are the inputs?
Also, check this out - https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!