Matlab Matrixaddress [duplicate] - matlab

This question already has answers here:
How to obtain the mirror image of an array (MATLAB)?
(4 answers)
Closed 2 years ago.
During my course I came across that expression:
A(:,end:-1:1)
I have trouble to understand and read the morphemic structure of the 2nd Operand "end;-1;1"
Lets take the example:
A=[1 2 3; 4 5 6; 7 8 9]
I am aware of:
A(:).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is :.
A(1,:).. Outputs [1 2 3; 4 5 6; 7 8 9] as columns Operator is , then , .
A(:,1).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is , beforehand : .
A(:,end:-1:1)
Output in Matlab show me: 3x3 Matrix.
How am I supposed to read the structure?
Graphem: : ..show me the rows,
Graphem: end:-1 .. ??
Graphem: :1 ..
Somehow ":" was for me the operator for show all of the elements.
It makes sense to me that the "Operand1 , Operand2" shows me the 2 Dimensional Matrix.
First Idea:
The end:-1:1 expression seemed to me like a loop. So -1, 0, 1 => **3x Elements** ?
But when I type
A(1,end:3)
it only shows me the 3rd row.
Second Idea:
A(end:-1:1,1)
It shows me the inverted Matrix..
My background:
I am a undergraduate student from the field of language.
I build in my freetime the 8-Bit Sap1 according Ben Eater.
So I am familiar with program memory or instruction memory.
I understand only the result, but not how it is achieved by the MATLAB compiler.
Someone said to me that the "Matrixaddressing is somehow optimized".
Looking forward to a helpful answer in each step. :)
Thanks in advance!

The end keyword in matrix indexing indicates index of last element in the corresponding dimension. So, A(:,end:-1:1) simply means A(:, size(A, 2):-1:1), which in you example (A=[1 2 3; 4 5 6; 7 8 9]), is equivalent to A(:, 3:-1:1).
But to understand what it does, you need to know what 3:-1:1 does. It creates a subrange. You already know 1:3 creates [1, 2, 3]. 1:3 is simplified form of 1:1:3: rangeStrart:increment:rangeEnd. Now, 3:1 or 3:1:1 creates an empty vector, because rangeStart is greater than rangeEnd. To create [3, 2, 1] you need to use a negative step: 3:-1:1.
So, A(:,end:-1:1) means A(:, [3, 2, 1]), which inverts order of rows of A. Also, A(:,end:3) means A(:, 3:3) and eventually A(:, 3), which returns 3rd row of A.
Edit: about your misunderstandings, addressed by #CrisLuengo
I am aware of:
A(:).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is :.
A(1,:).. Outputs [1 2 3; 4 5 6; 7 8 9] as columns Operator is , then , .
A(:,1).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is ,beforehand : .
A(3, 2) is the element in the 3,2 position (third row, second column) of A
A(1, :) is equivalent to A(1, 1:size(A, 2)) and A(1, 1:end) and is the first row of A
A(:, 1) is equivalent to A(1:size(A, 1), 1) and A(1:end, 1) and is the first column of A
A(:) is equivalent to A(1:numel(A)) and is a single column containing all elements of A

In MATLAB, when accessing an array, it is accessed as A(row#,col#). row# and col# can either be integers or a vector of integers. If they are integers then one spot in the matrix is accessed. If they are vectors then MATLAB will loop through the vector and choose spots in A which correspond to the integers in the vector.
end:-1:1 creates a vector which contains the integers ranging from the number of columns (in this case because you put this vector in the col section: A(row#,col#)) to 1. Ex: 4x5 matrix, end:-1:1 would be [5 4 3 2 1].
When you put : in the row part of the matrix, that means you access all rows of the matrix.
Here's an example of A(:,end:-1:1)
The col# vector (:) is [1 2 3] and the row# vector (end:-1:1) is [3 2 1]
A = [1 2 3;
4 5 6;
7 8 9]
A(:,end:-1:1)
[3, 5, 7]

I think you're overthinking this slightly.
If we have a vector
A = [1 2 3]
and we call A(end:-1:1), then we get a vector [3 2 1]. The indexing has returned the same vector, with the values reversed. If we now have a matrix
A = [1 2 3; 4 5 6; 7 8 9]
and call A(:, end:-1:1), we get the matrix with the same values in each row, but now the columns have been reversed to give
A(:, end:-1:1) = [3 2 1; 6 5 4; 9 8 7].
Recall what the colon means in this context.
If we define a vector, v = (1:10), we get a vector with the first element being 1, the last element being 10 and each value in between being integers in steps of 1. If we instead define v = (1:2:10), we get the same, but the elements are separated by 2, not 1.
end:-1:1 is a vector made in just the same way. The first number is the final element in the row of A, and the final number is the first element in the row. Each number is separated by a value of -1. If we try
v = 10:-1:1
we get [10 9 8 7 6 5 4 3 2 1]. If we call v(2:4) we get the second, third and fourth elements of v. If we call v(1:end), we simply get v. If we call v(end:-1:1), we return v, with the elements in the reversed order.
Edit A typo.

Related

Matlab: How to find indices of a specific value in vector

How can I find the indices of a specific value in a vector? For example in the following vector:
B = [2 3 4 5 2 7 9 2]
I need the index of all occurrences of 2, which is: [1 5 8]
find can be used for this purpose as follows:
find(B==2)
or an alternative:
ind = 1:numel(B);
ind(B==2)

MATLAB: Obtaining the rank ordering of a vector, with no tied ranks allowed

I've got a vector A = [6 5 7 7 4] and want to obtain the ranks as either [3 2 4 5 1] or [3 2 5 4 1] - I don't mind which. The answer is a vector in which each element is replaced by the rank it holds. This indicates to me that the fifth element is the smallest, then the second element is the second smallest, and so on.
I thought of doing [~,~,rnk] = unique(A), however that doesn't work, and produces instead [3 2 4 4 1].
How can I obtain the solution with no tied ranks?
It's almost a duplicate of this question.
We use sort twice, first sorting the array to get the index and then sort the index.
A = [6 5 7 7 4];
[~, rnk] = sort(A);
[~, rnk] = sort(rnk);
rnk =
3 2 4 5 1

Order of elements in cell array constructed by accumarray

What I'm trying to do: given a 2D matrix, get the column indices of the elements in each row that satisfy some particular condition.
For example, say my matrix is
M = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]
and my condition is M>6. Then my desired output would be something like
Indices = {[1 4]'; [2 3 4]'; [1 2 4]'; [2 3]';}
After reading the answers to this similar question I came up with this partial solution using find and accumarray:
[ix, iy] = find(M>6);
Indices = accumarray(ix,iy,[],#(iy){iy});
This gives very nearly the results I want -- in fact, the indices are all right, but they're not ordered the way I expected. For example, Indices{2} = [2 4 3]' instead of [2 3 4]', and I can't understand why. There are 3 occurrences of 2 in ix, at indices 3, 6, and 9. The corresponding values of iy at those indices are 2, 3, and 4, in that order. What exactly is creating the observed order? Is it just arbitrary? Is there a way to force it to be what I want, other than sorting each element of Indices afterwards?
Here's one way to solve it with arrayfun -
idx = arrayfun(#(x) find(M(x,:)>6),1:size(M,1),'Uni',0)
Display output wtih celldisp(idx) -
idx{1} =
1 4
idx{2} =
2 3 4
idx{3} =
1 2 4
idx{4} =
2 3
To continue working with accumarray, you can wrap iy with sort to get your desired output which doesn't look too pretty maybe -
Indices = accumarray(ix,iy,[],#(iy){sort(iy)})
Output -
>> celldisp(Indices)
Indices{1} =
1
4
Indices{2} =
2
3
4
Indices{3} =
1
2
4
Indices{4} =
2
3
accumarray is not guaranteed to preserve order of each chunk of its second input (see here, and also here). However, it does seem to preserve it when the first input is already sorted:
[iy, ix] = find(M.'>6); %'// transpose and reverse outputs, to make ix sorted
Indices = accumarray(ix,iy,[],#(iy){iy}); %// this line is the same as yours
produces
Indices{1} =
1
4
Indices{2} =
2
3
4
Indices{3} =
1
2
4
Indices{4} =
2
3

MATLAB: an efficient function that can group the unique numbers from a Vector

I'm looking for an efficient MATLAB function which is able to transform a vector to a cell with group of unique numbers
e.g
from
a=[1 1 5 5 5 2 1 1 1 2 2 6 6 6 8 8 8 8]
to
a={[1 1] [5 5 5] [2] [1 1 1] [2 2] [6 6 6] [8 8 8 8]}
My previous approach was to locate the changing points before running within a loop:
e.g.
change_pts = a-[0,a(1:length(a)-1)];
for i=find(ans~=0)
% codes here
end
But it's obviously not an optimised solution.
Thank you
You can create a vectorized solution using the functions diff and mat2cell like so:
a = mat2cell(a, 1, diff([0 find([(diff(a) ~= 0) 1])]));
This works by first locating where the values change and marking the ends of sequences of equal numbers with a 1. It then finds the indices of all the ones and computes the spans between these indices (i.e. the counts for each sequence of equal numbers). These counts are then used by mat2cell to break the vector up into a cell array.

Duplicate the first row and the first column of a matrix

I have a 3*3 matrix A
A = [1 2 3
4 5 6
7 8 9];
I want to duplicate only the first row and column of this matrix. It should look like
1 1 2 3
1 1 2 3
4 4 5 6
7 7 8 9
can someone tell how can i do this in matlab
I think this is a good way just using indexing
A([1, 1:end], [1, 1:end])
You can do that by concatenating different parts of the original matrix:
B=[A(1) A(1,:);A(:,1) A];
In this expression A(1) is the top left element of A, A(1,:) is the first row and A(:,1) is the first column.
See the documentation on the colon operator.
In the code below, A is your starting point and I believe E is what you want to achieve.
You can of course combine all the intermediate expressions to achieve the final result in one step.
A= [1 2 3; 4 5 6; 7 8 9]
B= A(1:3,1:1)
C= [B A]
D= C(1:1,1:4)
E= [D;C]
A bit late in the game, but worthwhile answering. You can use padarray for that :
B = padarray(A,[1 1],'replicate','pre')
It's a one liner and more generic if you want to add more than just a single first and column ...