Using ranges in Matlab/Octave matrices - matlab

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)

Related

average every n rows in a complex matrix

I have a big complex single matrix (9040 X 23293).
Because this matrix holds to much data for me, I want to average every n rows. For example, n can be 10 and the new matrix will be 904 X 23293.
I tried to use reshape but it does not work on complex numbers.
I would love to get some help.
Thanks,
Lauren
Thanks.
Laurn
Reshape works on complex numbers. As you did not share the code, I do'nt know what is the problem. Anyhow, if the number of rows is not multiple of 10, you can reshape first 10 * n rows and add the average of the remain rows. You can find the general solution in the following for the given complex matrix m:
fixed_num_rows = fix(size(m,1)/n);
means = mean(reshape(m(1:(fixed_num_rows * n),:), fixed_num_rows, n * size(m,2)),2);
means = [means; mean(mean(m((fixed_num_rows * n + 1):size(m,1),:)))];

how to calculate avrage of 1st 5 rows then next 5 row likewise continue in matlab

I have .xlsx file n rows and m column. I want to calculate average of 1st 5 rows then next 5 rows likewise ( each 5 interval average) continue for my data.
If A is your matrix, what about:
m=[];
for ii=1:5:20
m(end+1)=mean(mean(A(ii:ii+4,:)));
end
does it work for you?
Here is a "hack" that should be pretty fast if A is big:
m = mean(kron(speye(size(A,1)/5),ones(1,5))*A/5,2);
You can use reshape to get blocks of 5 rows in the first dimension. If A is your matrix
m = squeeze(mean(reshape(A,5,[],size(A,2)),1));
The code works as follows
reshape the matrix to get the blocks of 5 rows in the first dimension
Compute the mean over the blocks of 5 rows.
After the mean, the first dimension is a singleton so it is better to squeeze it so the output is a 2D matrix.

Get numbers in a matrix in different set positions

I have a matrix 1x5000 with numbers. Now I am interested in getting values from the matrix in different positions, more precisely in six different places of the matrix. The places should be based on the length, these are the numbers I want to get out:
Number in 1/6 of the matrix length
Number in 2/6 of the matrix length
Number in 3/6 of the matrix length
Number in 4/6 of the matrix length
Number in 5/6 of the matrix length
Number in 6/6 of the matrix length
These values could be printed out in another matrix, so assume the matrix is 1x5000, 3/6 would give the number in the middle of the matrix. I am new in Matlab and therefore the help is much appreciated!
Since your question is unclear I can try to give you an example.
First of all you can use numel function to get matrix's size.
It's easy to get necessary element in Matlab: you can address directly to any element if you know its number (index). So:
x(100) returns 100th element.
Now you got size and know what to do. Last moment - what to do if numel(x) / 6 return non integer?
You can use rounding functions: ceil, floor or round.
index = ceil(numel(x)/6) %if you want NEXT element always
result = x(index)
Next step: there are a lot of ways to divide data. For example now you have just 6 numbers (1/6, 2/6 and so on) but what if there are 1000 of them? You can't do it manually. So you can use for loop, or you can use matrix of indexes or perfect comment Stewie Griffin.
My example:
divider = [6 5 4 3 2 1] % lets take 1/6 1/5 1/4 1/3 1/2 and 1/1
ind = ceil( numel(x)./divider)
res = x(ind)
The colon notation in MATLAB provides an easy way to extract a range of elements from v:
v(3:7) %Extract the third through the seventh elements
You could either manually input range or use a function to convert fractions into suitable ranges

Determining if any duplicate rows in two matrices in MatLab

Introduction to problem:
I'm modelling a system where i have a matrix X=([0,0,0];[0,1,0],...) where each row represent a point in 3D-space. I then choose a random row, r, and take all following rows and rotate around the point represented by r, and make a new matrix from these rows, X_rot. I now want to check whether any of the rows from X_rot is equal two any of the rows of X (i.e. two vertices on top of each other), and if that is the case refuse the rotation and try again.
Actual question:
Until now i have used the following code:
X_sim=[X;X_rot];
if numel(unique(X_sim,'rows'))==numel(X_sim);
X(r+1:N+1,:,:)=X_rot;
end
Which works, but it takes up over 50% of my running time and i were considering if anybody in here knew a more efficient way to do it, since i don't need all the information that i get from unique.
P.S. if it matters then i typically have between 100 and 1000 rows in X.
Best regards,
Morten
Additional:
My x-matrix contains N+1 rows and i have 12 different rotational operations that i can apply to the sub-matrix x_rot:
step=ceil(rand()*N);
r=ceil(rand()*12);
x_rot=x(step+1:N+1,:);
x_rot=bsxfun(#minus,x_rot,x(step,:));
x_rot=x_rot*Rot(:,:,:,r);
x_rot=bsxfun(#plus,x_rot,x(step,:));
Two possible approaches (I don't know if they are faster than using unique):
Use pdist2:
d = pdist2(X, X_rot, 'hamming'); %// 0 if rows are equal, 1 if different.
%// Any distance function will do, so try those available and choose fastest
result = any(d(:)==0);
Use bsxfun:
d = squeeze(any(bsxfun(#ne, X, permute(X_rot, [3 2 1])), 2));
result = any(d(:)==0);
result is 1 if there is a row of X equal to some row of X_rot, and 0 otherwise.
How about ismember(X_rot, X, 'rows')?

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,:);