Two approaches you can use here.
Approach 1:
y = [1; 3; 2; 1; 3];
yvec = zeros(numel(y),3);
yvec(sub2ind(size(yvec),1:numel(y),y'))=1
Approach 2 (One-liner):
yvec = bsxfun(@eq, 1:3,y)
More Related Contents:
- Is vectorised data manipulation sequential (e.g R and MATLAB)?
- Which part of this is a non-negative integer? (factorial error) [closed]
- Finding islands of zeros in a sequence
- “Undefined function ‘function_name’ for input arguments of type ‘double’.”
- For loop to split matrix to equal sized sub-matrices
- Octave / Matlab: Extend a vector making it repeat itself?
- How can I plot a function with two variables in Octave or Matlab?
- Getting FFT peaks from data
- How can I apply a function to every row/column of a matrix in MATLAB?
- How to assign values to a MATLAB matrix on the diagonal?
- Why is reshape so fast? (Spoiler: Copy-on-Write)
- Multiply a 3D matrix with a 2D matrix
- pdist2 equivalent in MATLAB version 7
- Vectorizing the Notion of Colon (:) – values between two vectors in MATLAB
- Sweep / chirp signal ends at incorrect frequency
- How do I compare all elements of two arrays?
- Fast Algorithms for Finding Pairwise Euclidean Distance (Distance Matrix)
- How can I sort a 2-D array in MATLAB with respect to one column?
- How do I detect if I’m running MATLAB or Octave?
- Octave/Matlab: Adding new elements to a vector
- BSXFUN on memory efficiency with relational operations
- MATLAB: Subtracting matrix subsets by specific rows
- Efficiently compute a 3D matrix of outer products – MATLAB
- How to generate all pairs from two vectors in MATLAB using vectorised code?
- How can I accumulate cells of different lengths into a matrix in MATLAB?
- How should I update the data of a plot in Matlab?
- [caffe]: check fails: Check failed: hdf_blobs_[i]->shape(0) == num (200 vs. 6000)
- Return Unique Element with a Tolerance
- support vector machines in matlab
- How to plot a circle in Matlab?