Matlab programming code understanding - matlab

I have come across a matlab code that I am unable to understand. If anybody knows what this code means then help me in this regard.
Lambda(:,1) = [randi([1,4], 1,4), randi([1,30],1)*rand];
I do know that randi will return random integer between [min, max]. What I would like to know, that what lambda will receive? a row values, a column values or only a scalar value?

Well.. You could just run the code and see what happens:
[randi([1,4], 1,4), randi([1,30],1)*rand]
ans =
4.0000 2.0000 4.0000 1.0000 11.9046
So the answer will be: a row vector with 5 entries.
But let's look at it more detailed: With randi([1,4], 1,4) you create a row vector of size 1 x 4, containing random integers between [min,max], i.e. between 1 and 4. The second part similarly creates one integer in the range [1,30] and multiplies it by a random number from the interval (0,1).
With [x,y] you concatenate the two numbers or vectors. This leads to a row vector of size 1 x 5, as we saw in the beginning.
In the end you assign this to Lambda(:,1). As in MATLAB the first index is for rows and the second for columns, you select the first column of Lambda. You are thus trying to assign a 1 x 5 row vector to a 5 x 1 column vector. Luckily MATLAB is smart enough to handle that, so this snippet will work anyways. It would be a nicer and more clear solution, if you created a column vector instead of a row vector in the first place. That would be
Lambda(:,1) = [randi([1,4], 4,1); randi([1,30],1)*rand];

Related

Find entries with identical magnitude and sort them by the imaginary part in Matlab

I have a vector x with complex entries and I want to do the following:
Let's say there are entries with the same absolute value, for example 13, 5+12i and 5-12i. If I put those in the builtin sort function, it will put 13 at first, then 5-12i and then 5+12i. However, since all of those have the same magnitude, I want to sort them by their imaginary part, so I would like to have 5-12i come first, then 13 and then 5+12i. What is the easiest way of doing this, without using any loops or if statements?
You can construct a matrix that has the magnitude as the first column and the imaginary component as the second column and then use sortrows to sort the result which will sort first by the first column (the magnitude) and then by the second column (the imaginary component). You can use the second output of sortrows to get the corresponding indices of the sorted rows which you can then use to sort the original data.
data = [13, 5+12i, 5-12i];
[~, inds] = sortrows([abs(data(:)), imag(data(:))]);
sorted = data(inds);
% 5.0000 - 12.000i 13.0000 + 0.0000i 5.0000 +12.0000i

How to change elements in matrices using MATLAB

Starting wish a 7x4 binary matrix I need to change a random bit in each column to simulate error. Have been trying to no avail.
A very straightforward way to do this is to use a for loop. It might not be the most efficient approach in MATLAB, but it's probably good enough considering your data set is so small.
Iterate through each of the four columns. On each iteration, randomly chose a number from 1 to 7 to represent the row in that column that you have selected to change. Finally, flip the bit at that row/column. The following code does just this. Assume that "A" is a binary matrix with 7 rows and 4 columns
for col=1:4; %// Iterate through each column
row = ceil(7*rand()); %// Randomly chose a number from 1 to 7 to represent row
A(row,col) = ~A(row,col); %// Flip the bit at the specified row/col
end
Another possibility is to create 4 random numbers in one call, and assign in a vectorized fashion:
rowNumbers = randi(4,[1 4])
A(rowNumbers,:) = ~A(rowNumbers,:);

how this code is used in matlab for Artificial Intelligence

i am trying this code since last night but i cant understand what this code is doing. actually i am beginner in matlab programming
load('79.mat')
trainingData=d79;
colormap(gray);
colormap(grey);
x=reshape(d79(1234,:),28,28);
y = x(:,28:-1:1);
pcolor(y');
Kindly help me in understanding this code. :/
This is pretty simple. Here is a line-by-line explanation:
Loads data from a data file
Puts the loaded data into a variable named trainingData
Sets the colormap for plotting
Take the 1234th row of the loaded matrix, convert it into a 28-by-28 matrix, and store it in the variable x. So for example, on a smaller scale, [5 6 7 8] is converted to the matrix [5 7; 6 8] if you want to reshape that matrix to 2-by-2.
Reverse the column order of x and put that in y. So the last column becomes the first, second-last becomes second, etc.
This is a checkerboard plot of the values contained in y.
Edited to include more detail on lines 5 and 6:
The reshape line assumes that there is a row with number 1234, and that there are 784 (28*28) elements in that row. It takes that row, and makes a 28x28 matrix out of it, by taking the first 28 elements, making them into the first column of the new matrix, then taking the next 28 elements, making them the second column, and so on 26 more times. The final matrix is names x.
For the y line, like I said, it just reverses the columns of x, it puts the last column first, the second-last column second, and so on until the first column of the x is put as the last column of y.

Using ranges in Matlab/Octave matrices

Let's say I want to create an 100x100 matrix of which every row
contains the elements 1-100
A = [1:100; 1:100; 1:100... n]
Obviously forming a matrix is a bad idea, because it would force me to
create 100 rows of range 1:100.
I think I could do it by taking a 'ones' array and multiplying every
row by a vector... but I'm not sure how to do it
a = (ones(100,100))*([])
??
Any tips?
You can use the repeat matrix function (repmat()). You code would then look like this:
A = repmat( 1:100, 100, 1 );
This means that you're repeating the first argument of repmat 100 times vertically and once horizontally (i.e. you leave it as is horizontally).
You could multiply a column vector of 100 1s with a row vector of 1:100.
ones(3,1)*(1:3)
ans =
1 2 3
1 2 3
1 2 3
Or you could use repmat ([edit] as Phonon wrote a few seconds before me [/edit]).
Yes, repmat is the easy solution, and even arguably the right solution. But knowing how to visualize your aim and how to create something that yields that aim will give long term benefits in MATLAB. So try other solutions. For example...
cumsum(ones(100),2)
bsxfun(#plus,zeros(100,1),1:100)
ones(100,1)*(1:100)
cell2mat(repmat({1:100},100,1))
and the boring
repmat(1:100,100,1)

Size function in matlab

I am a new in MATLAB and I have a problem understanding the function size
in this statement: for i=1:size(scale,2) WHERE scale can be any integer number .e.g scale=5.
I found that in MATLAB help size(A,1) returns the number of rows of A , and
size(A,2) returns the number of columns of A.
Now I'm really confused as to what is the functionality of (size).
As you know, matlab deals mainly with matrices. So, the size function gives you the dimension of a matrix depending on how you use it. For example:
1. If you say size(A), it will give you a vector of size 2 of which the first entry is the number of rows in A and the second entry is the number of columns in A.
2. If you call size(A, 1), size will return a scalar equal to the number of rows in A.
3. If you call size(A, 2), size will return a scalar equal to the number of columns in A.
A scalar like scale in your example is considered as a vector of size 1 by 1. So, size(scale, 2) will return 1, I believe.
Hope this clarifies.
The Linear Algebra operations in Matlab/octave by default follow Row-Column order (ie they are row major by default); so if A is a matrix of size 3x2 (3 rows and 2 columns), we can use size to determine the order of matrix/vector
size(A) will return 3 2 (the first entry representing no.of rows & the second one is no.of columns). Similarly,
size(A,1) returns 3 (1 here represents the no. of rows and A has 3 rows)
size(A,2) returns 2 (2 here represents the no. of columns and A has 2 columns)