Access Elements of Array in MATLAB - matlab

I am trying to access some elements of an array in matlab. Consider the scenario below :
a = [1 2 3;4 5 6;7 8 9]
b = [1 2;2 1]
I want to access elements with indices (1,2) and (2,1) from a. I tried using a(b) etc. But none of the methods I tried worked.
How can this be done in matlab without using loops?
Also it would be helpful if you could suggest some good books for such basics in matlab.

First, convert your subscripts to indices using sub2ind:
dim1sub = b(:,1);
dim2sub = b(:,2);
ind = sub2ind(size(a), dim1sub, dim2sub)
After you have the indices
a(ind)
will give you:
ans =
2
4
See here for more information on matrix indexing.

Matlab lets you access a matrix with a linear index that scans through all the columns of the matrix. So in your case (with a 3x3) a(2,1)=a(2) and a(1,2)=a(4). The answer that #HebeleHododo provided takes your row and column index and converts them into a linear index into matrix a. Just remember that if you want to index a different size matrix, you will need a different linear index for it.
Also, there is a lot of information available online to help with learning matlab at http://www.mathworks.com/help/matlab/index.html#language-fundamentals or you can type doc help into the command window

Related

Matrix indexing using vectors in Matlab [duplicate]

This question already has an answer here:
MATLAB one liner for batch assignment in 2D matrix?
(1 answer)
Closed 7 years ago.
So I'm a beginner in MATLAB so this question might be trivial.
Suppose x=[1 2 3 4 5] and y=[3 4 2 5 1] and img = zeros(5,5). I want to set img(1,3),(2,4),(3,2),(4,5),(5,1) to 1. How do I do this? When I simply try img(x,y), it takes all the combinations of indices like (1,3),(1,4),(1,2) etc. which is not what I want.
As you have experienced, MATLABs indexing doesn't work that way. To get a feeling for the ways indexing works in MATLAB, please have a look at this nice article from Mathworks.
Now how to tackle your problem: The solution is to use linear indexing. You can always index a 2-dimensional matrix either by (i,j) or with a linear index k which increases column-wise. You can convert between matrix-indexes and linear indexes using the sub2ind function. To get the correct indexes for your questions, use
img = zeros(5,5)
x = [1 2 3 4 5];
y = [3 4 2 5 1];
ind = sub2ind(size(img),x,y);
img(ind) = 1;

quickly find the indexes of common rows in two matrices

Thanks in advance for the help.
Suppose that I have two matrices: A and B. I want to know which rows in A are also in B. For example given
A = [1 2; 3 4] and B = [1 2; 5 6; 7 8]
I would like an output of
out = [1 0];
A simple way of doing this is to use for loops but my A and B matrices are both very large. Using for loops is thus exceedingly slow (it would likely take several hours to handle just two matrices and I have several thousand to compare). Is there a way that I could do this using Matlab's built-in functions (which are optimized to handle matrix operations)?
There is a way to do it with MATLAB's built-in functions!
out = ismember(A, B, 'rows');

How to transpose a matrix without MATLAB's transpose tool

I have an image, which I have listed as a matrix. I want to take the transpose of that image and then display that image on the screen.
I would like to know how to do this the "hard" way, ie without using MATLAB's transpose function.
function [B] = trans(A)
[r c] = size(A);
B = zeros(c,r);
for i = 1:r
for j = 1:c
B(j,i) = A(i,j)
end
end
end
As this is for a class, I won't give you an exact answer, but I will nudge you in the right direction. I'm assuming that you are looking for a method that involves manually transposing the information, rather than using builtin functions.
Matlab stores values in a matrix in the form of a vector and a "size" - for instance, a 2x3 matrix would be stored with six values in a vector, and then [2,3] (internally) to tell it that it's 2x3 and not 6x1.
For the 2x3 matrix, this is the order of the values in the vector:
1 3 5
2 4 6
To reference the value in (2,2), you can reference it as A(2,2), or as A(4). The value in (1,3) can be referenced as A(5).
As such, if you can construct a vector referencing the values in the transposed order, then you can assign the new values into the appropriate order and store them in a matrix of appropriate size. To make the point, consider the transpose of the above matrix:
1 2
3 4
5 6
This would be represented as (1,3,5,2,4,6) with size (3,2). If you can construct the vector (1,3,5,2,4,6), then you can use that vector to assign the values appropriately.
Here's hint toward a solution: The transpose transforms the element A(1,2) from the normal array A into the element B(2,1) from the transposed array B (B=A'), and vice versa. Thus, you can iterate through all the rows and column, and apply the transformation element-by-element.
Are you allowed to use flipud and fliplr ?
In this case you can represent the transposition as a sequence of flips (I'll leave it to you to figure out the exact sequence).
You can use rot90 with flipud/fliplr for this.

matlab array subsetting by indices

Let's say I have a 3x3x3 Matlab array with members 1 to 27
a=reshape(1:27, [3 3 3])
I would like to create a subset of this with a syntax like
b=a(range1,range2,range3)
where for range1=range2=range3=1:2 I would get the members b(1,1,1) and b(2,2,2). i.e
b= [1 14]
Is it possible to do this just with indexing and without any functions (e.g. diag)? Thanks...
It can be done with sub2ind function as follows:
b=a(sub2ind(size(a),range1,range2,range3))
ans: b=[1 14]
The indexing can be done using sub2ind,
a(sub2ind(size(a),[1:2],[1:2],[1:2]))
if you want to avoid all functions, you could calculated the linear indices yourself...

How to find the row rank of matrix in Galois fields?

Matlab has a built-in function for calculating rank of a matrix with decimal numbers as well as finite field numbers. However if I am not wrong they calculate only the lowest rank (least of row rank and column rank). I would like to calculate only the row rank, i.e. find the number of independent rows of a matrix (finite field in my case). Is there a function or way to do this?
In linear algebra the column rank and the row rank are always equal (see proof), so just use rank
(if you're computing the the rank of a matrix over Galois fields, consider using gfrank instead, like #DanBecker suggested in his comment):
Example:
>> A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
>> rank(A)
ans =
2
Perhaps all three columns seem to be linearly independent, but they are dependent:
[1 2; 4 5] \ [3; 6]
ans =
-1
2
meaning that -1 * [1; 4] + 2 * [2; 5] = [3; 6]
Schwartz,
Two comments:
You state in a comment "The rank function works just fine in Galois fields as well!" I don't think this is correct. Consider the example given on the documentation page for gfrank:
A = [1 0 1;
2 1 0;
0 1 1];
gfrank(A,3) % gives answer 2
rank(A) % gives answer 3
But it is possible I am misunderstanding things!
You also said "How to check if the rows of a matrix are linearly independent? Does the solution I posted above seem legit to you i.e. taking each row and finding its rank with all the other rows one by one?"
I don't know why you say "find its rank with all the other rows one by one". It is possible to have a set of vectors which are pairwise linearly independent, yet linearly dependent taken as a group. Just consider the vectors [0 1], [1 0], [1 1]. No vector is a multiple of any other, yet the set is not linearly independent.
Your problem appears to be that you have a set of vector that you know are linearly independent. You add a vector to that set, and want to know whether the new set is still linearly independent. As #EitanT said, all you need to do is combine the (row) vectors into a matrix and check whether its rank (or gfrank) is equal to the number of rows. No need to do anything "one-by-one".
Since you know that the "old" set is linearly independent, perhaps there is a nice fast algorithm to check whether the new vector makes thing linearly dependent. Maybe at each step you orthogonalize the set, and perhaps that would make the process of checking for linear independence given the new vector faster. That might make an interesting question somewhere like mathoverflow.