Is there a matlab command for generating a random n by n matrix, with elements taken in the interval [0,1], with x% of the entries on the off-diagonal to be 0. Then, additionally setting the element in the diagonal to be the sum of every element in its respective column? In order to create a diagonally dominant dense/sparse matrix? This may be easy enough to write a code for but I was wondering if there was already a built in function with this capability.
EDIT:
I am new to Matlab/programming so this was an easier said than done. I'm having trouble making the matrix with the percentage ignoring the diagonal. It's a n x n matrix, so there are $n^2$ entries, with n of them on the diagonal, I want the percentage of zeros to be taken from $n^2 - n$ elements, i.e. all the off-diagonal elements. I cannot implement this correctly. I do not know how to initialize my M (see below) to correspond correctly.
% Enter percentage as a decimal
function [M] = DiagDomSparse(n,x)
M = rand(n);
disp("Original matrix");
disp(M);
x = sum(M);
for i=1:n
for j=1:n
if(i == j)
M(i,j) = x(i);
end
end
end
disp(M);
Here is one approach that you could use. I'm sure you will get some other answers now with a more clever approach, but I like to keep things simple and understandable.
What I'm doing below is creating the data to be put in the off-diagonal elements first. I create an empty matrix and copy this data into the off-diagonal elements using linear indexing. Now I can compute the sum of columns and write those into the diagonal elements using linear indexing again. Because the matrix was initialized to zero, the diagonal elements are still zero when I compute the sum of columns, so they don't interfere.
n = 5;
x = 0.3; % fraction of zeros in off-diagonal
k = round(n*(n-1)*x); % number of zeros in off-diagonal
data = randn(n*(n-1)-k,1); % random numbers, pick your distribution here!
data = [data;zeros(k,1)]; % the k zeros
data = data(randperm(length(data))); % shuffle
diag_index = 1:n+1:n*n; % linear index to all diagonal elements
offd_index = setdiff(1:n*n,diag_index); % linear index to all other elements
M = zeros(n,n);
M(offd_index) = data; % set off-diagonal elements to data
M(diag_index) = sum(M,1); % set diagonal elements to sum of columns
To refer to the diagonal you want eye(n,'logical'). Here is a solution:
n=5;
M = rand(n);
disp("Original matrix");
disp(M);
x = sum(M);
for i=1:n
for j=1:n
if(i == j)
M(i,j) = x(i);
end
end
end
disp('loop solution:')
disp(M);
M(eye(n,'logical'))=x;
disp('eye solution:')
disp(M);
Related
I would like to generate 100 random matrices A=[a_{ij}] of size 6 by 6 in (0, 9) using matlab programming satisfying the following properties:
1. multiplicative inverse: i.e., a_{ij}=1/a_{ji} for all i,j=1,2,...,6.
2. all entries are positive: i.e., a_{ij}>0 for all i,j=1,2,...,6.
3. all diagonal elements are 1: i.e., a_{ii}=1 for all i=1,2,..,6.
4. transitive: i.e., a_{ih}*a_{hj}=a_{ij} for all i,j,h=1,2,...,6.
So far, I tried to use a matlab function rand(6)*9. But, I got wrong matrices. I was wondering if anyone could help me?
Here is my matlab code:
clc; clear;
n=6;
m=0;
for i=1:n
for j=1:n
for h=1:n
while m<100 % generate 100 random matrices
A=rand(n)*9; % random matrix in (0,9)
A(i,j)>0; % positive entries
A(i,j)==1/A(j,i); % multiplicative inverse
A(i,h)*A(h,j)==A(i,j); % transitive
if i==j && j==h
A(i,j)==1; % diagonal elements are 1
break;
end
m=m+1;
M{m}=A
end
end
end
end
M{:}
clear; clc
M = cell(1, 100); % preallocate memory
% matrix contains both x & 1/x
% we need a distribution whose multiplication with its inverse is uniform
pd = makedist('Triangular', 'a', 0, 'b', 1, 'c', 1);
for m=1:100 % 100 random matrices
A = zeros(6); % allocate memory
% 5 random numbers for 6x6 transitive random matrix
a = random(pd, 1, 5);
% choose a or 1/a randomly
ac = rand(1, 5) < 0.5;
% put these numbers above the diagonal
for i=1:5
if ac(i)
A(i, i+1) = a(i);
else
A(i, i+1) = 1 / a(i);
end
end
% complete the transitivity going above
for k=flip(1:4)
for i=1:k
A(i, i-k+6) = A(i, i-k+5) * A(i-k+5, i-k+6);
end
end
% lower triangle is multiplicative inverse of upper triangle
for i=2:6
for j=1:i-1
A(i,j) = 1 / A(j,i);
end
end
c = random(pd); % triangular random variable between (0,1)
A = A ./ max(A(:)) * 9 * c; % range becomes (0, 9*c)
% diagonals are 1
for i=1:6
A(i,i) = 1;
end
% insert the result
M{m} = A;
end
There are actually 5 numbers are independent in 6x6 transitive matrix. The others are derived from them as shown in the code.
The reason why triangular distribution is used for these numbers is because pdf of triangular distribution is f(x)=x, and pdf of inverse triangular distribution is f-1(x)=1/x; thus their multiplication becomes uniform distribution. (See pdf of inverse distribution)
A = A ./ max(A(:)) * 9; makes the range (0,9), but there will always be 9 as the maximum element. We need to shrink the result by a random coefficient to obtain the result uniformly distributed in (0,9). Since A is uniformly distributed, we can achieve this again by triangular distribution. (See product distribution)
Another solution to the range issue would be calculating A while its maximum is above 9. This would eliminate the latter problem.
Since all elements of A depends on 5 random variables, the distribution of them will never be perfectly uniform, but the aim here is to maintain a reasonable scale for them.
It took me a litte to think about your question, but I realized there is no solution.
You require your elements of A to be uniformly distributed in the (0,9) range. You also require a_{ij}*a_{jk}=a_{ik}. Since the product of two uniform distributions is not a unifrom distribution, there is no solution to your question.
I need to plot a NxN matrix 'M' full of zeros, but only show the cases where m(x,y) is different from 0.
t_max = 10; % set the maximum number of iterations
n = 10; % dimension n*n
d = 1; % the probability of changing place
x = randi([1 n]); % random row
y = randi([1 n]); % random column
grid = zeros(10); % set an empty gride n*n
grid(x,y) = 1; % put an agent in a random place
for t=1:t_max
newgrid = randomwalk1(grid,d); % call the function random walk for one agent
end
I tried image(m) but it's not giving satisfying results since I need also to keep track of the element that is different to 0, hold on doesn't work in this case.
You are looking for the spy() function. Just type spy(m) and see what happens.
I am constructing an adjacency list based on intensity difference of the pixels in an image.
The code snippet in Matlab is as follows:
m=1;
len = size(cur_label, 1);
for j=1:len
for k=1:len
if(k~=j) % avoiding diagonal elements
intensity_diff = abs(indx_intensity(j)-indx_intensity(k)); %intensity defference of two pixels.
if intensity_diff<=10 % difference thresholded by 10
adj_list(m, 1) = j; % storing the vertices of the edge
adj_list(m, 2) = k;
m = m+1;
end
end
end
end
y = sparse(adj_list(:,1),adj_list(:,2),1); % creating a sparse matrix from the adjacency list
How can I avoid these nasty nested for loops? If the image size is big, then its working just as disaster. If anyone have any solution, it would be a great help for me.
Regards
Ratna
I am assuming the input indx_intensity as a 1D array here. With that assumption, here's a vectorized approach with broadcasting/bsxfun -
%// Threshold parameter
thresh = 10;
%// Get elementwise differentiation between elements in indx_intensity
diffs = abs(bsxfun(#minus,indx_intensity(:),indx_intensity(:).')) %//'
%// Threshold the differentiations against the threshold, thus giving us a
%// 2D square matrix. Then, set the diagonal elements to zero to avoid them.
mask = diffs <= thresh;
mask(1:len+1:end) = 0;
%// Get the indices of the TRUE elements in the valid mask as final output.
[R,C] = find(mask);
adj_list_out = [C R];
I was trying out something in an assignment I had. I wanted to subtract a row vector from every row of matrix(and then do further computations on it).
I have a matrix "X" of dimensions m X n and another one centroid of dimension K x n. I tried two varients,
function idx = findClosestCentroids(X, centroids)
K = size(centroids, 1);
m=size(X,1);
n=size(X,2);
idx = zeros(size(X,1), 1);
% ====================== CODE ======================
% Instructions: Go over every example, find its closest centroid, and store
% the index inside idx at the appropriate location.
% Concretely, idx(i) should contain the index of the centroid
% closest to example i. Hence, it should be a value in the
% range 1..K
%This Doesnt Work
% for i=1:m
% temp=ones(size(centroids))*diag(X(i))-centroids;
% temp=temp.^2;
% [x,y]=min(sum(temp,2));
% idx(i)=y;
% endfor
%This works!
for i=1:m
mini=1e10;
minin=-1;
for j=1:K
temp=X(i,:)-centroids(j,:);
temp=temp.^2;
sumx=sum(temp);
if (sumx<mini)
mini=sumx;
minin=j;
endif
endfor
idx(i)=minin;
endfor
% =============================================================
end
The first one works while the second one doesn't even though acc. to what I tried, second is just the vectorized version of the first one. Please guide me through the vectorization.
If you are looking for a MATLAB implementation, think this might serve your vectorization needs -
%%// Spread out centroids to the third dimension so that the singleton
%%// second dimension thus created could be used with bsxfun for expansion in
%%// that dimension
centroids1 = permute(centroids,[3 2 1]);
%%// Perform the much-needed subtraction
t1 = bsxfun(#minus,X,centroids1)
%%// Perform element-wise squaring and then min-finding as required too
t2 = t1.^2
t3 = sum(t2,2)
%%// Since the expansion resulted in data in third dimension, min-finding
%%// must be along it
[mini_array,idx] = min(t3,[],3)
Also, allow me to suggest an edit in your loop code. If you are interested in storing the minimum values as well at the end of each outer loop iteration, you might as well store it with something like min_array(i) = mini, just like you did when storing the indices.
Less parallelized than the other answer but much more easier to read:
for i = 1:size(X,1)
% z = centroids .- X(i, :); // Only Octave compatible
z = bsxfun(#minus, centroids, X(i, :));
zy = sum(z.^2, 2);
[~, idx(i)] = min(zy);
end
I need to create a list (of size n) of random, non-repeating set of coordinates on a matrix of predefined size.
Is there a fast way to generate this in Matlab?
My initial idea was to create a list of size n with permutations the size of (width x length) and to translate them back to Row and Col values, but it seems to me too much.
Thanks,
Guy
You can use randperm to generate a linear index, and convert it to [row,col] if needed using ind2sub.
x = rand(7,9);
n = 20;
ndx = randperm(numel(x), n);
[row,col] = ind2sub(size(x), ndx);
As long as n is less than the number of elements in the matrix this is simple:
% A is the matrix to be sampled
% N is the number of coordinate pairs you want
numInMat = numel(A);
% sample from 1:N without replacement
ind = randperm(numInMat, N);
% convert ind to Row,Col pairs
[r, c] = ind2sub( size(A), ind )
Your idea is a good one, although you don't even have to convert your linear indices back to row and col indices, you can do linear indexing directly into a 2D array.
idx = randperm(prod(size(data)))
where data is your matrix. This will generate a vector of random integers between 1 and prod(size(data)), i.e. one index for each element.
e.g.
n = 3;
data = magic(n);
idx = randperm(prod(size(data)));
reshape(data(idx), size(data)) %this gives you your randomly indexed data matrix back