Extract data and rename each iteration in loop [duplicate] - matlab

This question already has answers here:
How to automatically create variables which are column extracts from a matrix
(4 answers)
MATLAB - Need to split a Matrix into column variables with names sourced from another matrix?
(3 answers)
Closed 5 years ago.
I have created a color map:
Color map=jet(40)
From this I want to use a loop to extract each row of the colormap as 40 separate matrix (vector) with the title rgb1-rgb40. How to do this?

You can use eval to execute a dynamically created string as follow:
map=jet(40)
for i=1:size(map, 1)
eval(['rgb', num2str(i), '= map(', num2str(i), ', :)']);
end
Warning: Note that converting your matrix to 40 vectors in this way is probably not the most elegant solution for what you try to obtain. For more information, see Alternatives to the eval Function.

Related

MATLAB - Repeat row n times (from A=[1;2;3] create B=[1;2;3;1;2;3;1;2;3]) [duplicate]

This question already has answers here:
Building a matrix by merging the same row vector multiple times
(2 answers)
Closed 5 years ago.
I would like to create, from the column matrix A=[1;2;3], another column matrix that repeats A n times. For example, being n=3, the new matrix would be B=[1;2;3;1;2;3;1;2;3]. Is there a way to to that (preferably without using loops)?
Thank you.
You can use repmat it is a fantastic function:
repmat(A,[n,1])
The first value of the second parameter is repetitions in the first dimension (columns), the second in the second dimension (rows) etc.
Another way to do it:
A2=A(:,ones(1,n));
B=A2(:)
Another way is to do using padarray.
a = [1 2 3]
b = padarray(a, [2 0], 'post', 'circular')
post means add to the end of the array, circular pads with circular repetition of elements.

How do I turn 3x4x81x97 matrix into (long) column vector in Matlab [duplicate]

This question already has answers here:
How do you concatenate the rows of a matrix into a vector?
(2 answers)
Closed 6 years ago.
I have a data matrix (XW_region) that is size 3x4x81x97. Put differently, XW_region is indexed as (day,time,lat,lon), so there are 4 lat/lon grids (i.e. maps, populated by XW_region values) per day for 3 days, leading to 12 lat/lon grids total.
e.g. size(XW_region) = 3 4 81 97
What I want to do is take each XW_region value from each grid cell from each time from each day, and put them into one (long) column vector. From there I want to create a boxplot of the data. I know how to do the boxplot, just need to get the data all combined into one column vector.
Do I need to use the squeeze function to break out each map by day and time?
Thanks!
I'm sure this is a duplicate somewhere, but this is probably what you want:
XW_region(:)
see more about the column operator here.
As well as #bla's perfectly correct answer, sometimes it's useful to use reshape instead:
reshape(XW_region, [], 1);
(This pattern is helpful in cases where the thing you want to turn into a column is already an expression involving indexing).

Is there anyway to avoid loop in matlab array accessing? [duplicate]

This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I have a 2-D array. And I want to access it with rows and colums index stored in another 2-D array.
Example: Now I don't want to use loops but I want to access A(1, 2) and A(3, 4).
A = ones(10,10)
B = [1, 2 ; 3, 4]
If I do A(b(:,1), b(:,2)), this will result in all possible combination of [1,2] and [3,4].
How can it be done?
Use MATLAB's sub2ind function:
A(sub2ind(size(A),B(:,1),B(:,2)))

How to create a list of lists with unequal number of elements in matlab? [duplicate]

This question already has an answer here:
How to store this structure (list of lists of integers) in Matlab?
(1 answer)
Closed 7 years ago.
I want to create list of the following type:
[ [1,2,3], [5,6] , [1,2,4,6,7,89,9,74,4]]
But whenever I do, matlab automatically concatenates the list. Can anyone help me out?
What you want is called cell array, you can create one by doing:
A = {[1,2,3], [5,6], [1,2,4,6,7,89,9,74,4]};
To access cell elements you have to use the {} operator. As #TroyHaskin said, you can find a most complete answer here

How can I repeat matrix? [duplicate]

This question already has answers here:
how to replicate an array
(4 answers)
Closed 8 years ago.
Could you help me please to repeat matrix. for example if I have matrix(A) and I want to create a big matrix(B) contains three matrices of matrix(A) in the row and two matrices in the column.
The MATLAB function repmat does exactly what you need:
B = repmat(A,3,2);
For more details see the MATLAB documentation
In MatLab, you want to use repmat().
In Python, use the Numpy function, tile(a, (m, n)).
You should check out this post: https://stackoverflow.com/a/1724410/515559