In Matlab, say that I have a 3x1 vector of ones. Then I want to do a for loop, changing one element to a zero. So that I get (0,1,1), (1,0,1) and (1,1,0) from my loop. How can I go about this?
I have tried
for i = s
i = 0;
print(s);
end
where s is my vector, but it doesn't work. Note that I'm a beginner at programming.
Thank you!
Instead of showing what's wrong with your code, I'll show you a more Matlab-like way to do it:
n = 3; %// problem size
matrix = ones(n)-eye(n); %// n x n matrix with all ones except zeros at the diagonal
for k = 1:n %// pick each row
disp(mat2str(matrix(k,:))) %// create a string from n-th row and display it
end
Result:
[0 1 1]
[1 0 1]
[1 1 0]
Related
I have an N x N matrix, A, and a vector of row indices, v. I want to replace the diagonal elements of A only for the rows in A specified by v without using a for loop.
For example:
N = 10;
A = rand(N,N); %Random N x N matrix
v = [1 4 6 9 10]; %vector of row indices
%What I want to do but without a for loop:
for i = 1:length(v)
A(v(i),v(i)) = 0;
end
%I thought this would work, but it does not:
%A(v,v) = 0;
I feel like there must a one-line method of doing this, but can't seem to figure out what it would be.
Cheers
Use sub2ind:
A(sub2ind(size(A),v,v)) = 0;
I have many array (n*1 dimension), how can I do something like
matrix = [];
for i = 1:5
for j =1:5
matrix (i,j) = zeros(n,1); % store a given array to a cell of a matrix
end
end
I find Array of Matrices in MATLAB
But this is store matrices into array, not the otherwise.
Ying Xiong's suggestion is what you want if the vectors are of different lengths. But assuming the number of elements is constant (which they seem to be) you may also use a 3-dimensional array, where each (i,j) element contains a vector in the third dimension, like this:
rows = 5; cols = 5; n = 10; %// Dimensions
matrix = zeros(rows, cols, n); %// Initialize matrix
vector = 1:n; %// Just an example
for ii = 1:rows %// Bad practice to use i as a variable name
for jj = 1:cols %// Bad practice to use j as a variable name
matrix(ii,jj,:) = vector; %// Assignment
end
end
Now each index (i,j) contains the vectors you want, for instance:
squeeze(matrix(1,1,:))
ans =
1
2
3
4
5
6
7
8
9
10
Having all values in a single matrix can be a good thing if you want to do similar operations on all elements, as vectorized approaches are usually very fast in MATLAB. You might want to check out permute, reshape and functions like bsxfun.
Note that you might be able to vectorize the loops, but without knowing the specifics, that's impossible to know.
You need to use cell array.
n = 10;
matrix = cell(5,5);
for i = 1:5
for j = 1:5
matrix{i,j} = zeros(n,1);
end
end
I have a vector of length 3. i want to represent it as a matrix of dimension 4*2. ie) if the length of vector is n then matrix should be of dimension (n+1)*2. The matrix should have elements arranged as follows:
Vector= [2 3 4]
Matrix = [0 2;2 3;3 4;4 0]
You can solve your problem easily with simple operations:
vector = [2 3 4];
matrix = [0 vector; vector 0]';
' is used to transpose the matrix.
Additionally there are two useful functions in Matlab to manipulate matrices and vectors:
reshape()
repmat()
The command reshape from Matlab is the basis of my answer to your question:
B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements (from the official Matlab help).
You basically add zeros at the beginning and at the end and then have every number in the vector occur twice (if you "unfold"/reshape the matrix). So lets construct the desired matrix by reversing this description:
%set input vector
v = [2 3 4];
%"double" the numbers, v_ is my temporary storage variable
v_ = [v; v];
%align all numbers along one dimension
v_ = reshape(v_, 2*length(v), 1)
%add zeros at beginning and end
v_ = [0 v_ 0];
%procude final matrix
m = reshape(v_, length(v)+1, 2);
in short
%set input vector
v = [2 3 4];
%"double" the numbers, v_ is my temporary storage variable
%all values are aligned as row vector
%zeros are added at beginning and end
v_ = [0, v, v, 0];
%produce final matrix
m = reshape(v_, length(v)+1, 2);
I haven't checked it, since I don't have a Matlab at hand right now, but you should get the idea.
Edit
The answer by 13aumi manages this task even without the reshape command. However, you need to pay close attention to the shape of v (row- vs- column-vector).
I'm new to Matlab and I'm trying to solve a problem that involves creating a d dimensional multiplication table where each edge goes from 1 to n. The problem statement says that inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.
Ideally, I would just create a matrix of 1 to n along d dimensions and then iterate through for each element setting it equal to the product of the indices, but I don't know how to create the d dimensional matrix.
Can anyone help me with this problem?
You can create the table with repeated use of bsxfun. At each iteration, the vector 1,2,...,n is shifted to a new dimension and multiplied (with singleton expansion) by the previous result.
%// Data
d = 3;
n = 10;
%// Computations
vector = (1:n).'; %// first dimension: column vector
result = 1; %// initialization
for n = 1:d
result = bsxfun(#times, result, vector); %// new dimension
vector = shiftdim(vector,-1); %// shift to the next dimension
end
For example, given i=5 and and n=8, I want to generate [0;0;0;0;1;0;0;0]. Specifically, I want to generate the vector v so that:
v = zeros(n,1);
v(i) = 1;
Is there a (reasonable) way to do this in one line?
Another solution:
I = eye(n);
v = I(:, i);
Actually, you can have a vector y of numbers from 1 to n and get vectors like this for each element:
v = I(:, y);
You can see my blog post for the details on this general solution.
One way is [1:8]'==5, or more generally [1:n]'==i
Here's another solution using sparse to create an n length row vector with a 1 in the ith position:
v = sparse(1,i,1,1,n)
The advantage is that for large n, this is also memory efficient and can be used as usual in matrix calculations. If you ever need the full vector, just use full.
Here is another one:
n = 8;
p = 4;
arrayfun(#str2double,dec2bin(2^(p-1),n))
And another one (Creates a row vector):
circshift( [1 zeros(1,n-1)],[0 p]);
Or a column vector:
circshift( [1 ; zeros(n-1,1)],[p 0]);
Here is another one:
subsref( eye(n), struct('type','()', 'subs',{{ p,':' }}))