I have a 1D array (say A) of size N (i.e N x 1; N-rows, 1 Column). Now I want to create an array of size N x 2 (N-rows, 2-columns) with the array A as one column and the other column with a same element (0 in the given example below).
For e.g If
A =[1;2;3;4;5];
I'd like to create a matrix B which is
B=[0 1; 0 2; 0 3; 0 4; 0 5]
How do I do this in Matlab?
You can also abuse bsxfun for a one-liner -
bsxfun(#times,[0,1],A)
Or matrix-multiplication for that implicit expansion -
A*[0,1]
You can initialize B to be an Nx2 array of all zeros and then assign the second column to the values in A.
A = [1;2;3;4;5];
B = zeros(numel(A), 2);
B(:,2) = A;
% 0 1
% 0 2
% 0 3
% 0 4
% 0 5
If you actually just want zeros in that first column, you don't even have to initialize B as MATLAB will automatically fill in the unknown values with 0.
% Make sure B isn't already assigned to something
clear B
% Assign the second column of uninitialized variable B to be equal to A
B(:,2) = A;
You can try this approach
B=[zeros(length(A),1) A]
Related
I have a large array A, in which I wish to insert values at specific positions. These values and positions are stored in array B. Can I execute this assignment using indexing, i.e. without resorting to a for-loop or arrayfun?
Example:
% target array
A = [NaN NaN;
NaN NaN];
% r c value
B = [1 1 17;
1 2 13;
2 2 21];
% ?? Mystery operation ??
% Desired result:
A = [17 13;
NaN 21];
This is a vectorize solution:
accumarray(B(:,1:2),B(:,3),[],[],NaN)
referring to documentation of accumarray we can use signature accumarray(subs,val,sz,fun,fillval) that first two arguments are subs= [row and column indices] and val=value of matrix and 5th argument fillval:
fills all elements of A that are not referred to by any subscript in subs with the scalar value fillval
so we use NaN for the 5th argument.
or you can use signature accumarray(subs,val,sz,fun,fillval,issparse) if you want to create sparse matrix.
Solution
Another approach might be with the use of linear indices:
% Define data
A = [NaN NaN;
NaN NaN];
B = [1 1 17;
1 2 13;
2 2 21];
% Create linear indices for matrix A, with rows from B(:, 1) and columns from B(:, 2)
indices = sub2ind(size(A), B(:, 1), B(:, 2));
% Replace the data in A with values from B(:, 3)
A(indices) = B(:, 3);
I have a vector like
A=[1,2,3,4,5,6,7,8,9,10];
I would like to insert 2 zero every 5 number.
The result would be A=[1,2,3,4,5,0,0,6,7,8,9,10,0,0].
I know I could preallocate the space and then use a for cycle to assign the variable, but I was wandering if there was some more elegant way.
This works even if A doesn't contain an integer number of blocks:
A = [1,2,3,4,5,6,7,8,9,10,11,12]; % input vector
m = 5; % block size
n = 2; % number of zeros to be added after each block
B = zeros(1, numel(A)+floor(numel(A)/m)*n); % preallocate to appropriate size
B(mod(0:end-1, m+n)<m) = A; % logical index. Fill values of A at desired positions of B
The result in this example is
B =
1 2 3 4 5 0 0 6 7 8 9 10 0 0 11 12
With A having number of elements a multiple of 5, you could use some reshaping and concatenation with zeros, like so -
reshape([reshape(A,5,[]) ; zeros(2,numel(A)/5)],1,[])
Suppose I have a matrix A [m x 1], where m is not necessarily even. I to create a matrix B also [m x 1] which tells me the decile of the elements in A (i.e. matrix B has numbers from 1 to 10).
I know I can use the function sort(A) to get the position of the elements in A and from there I can manually get deciles. Is there another way of doing it?
I think one possibility would be B = ceil(10 * tiedrank(A) / length(A) . What do you think? Are there any issues with this?
Also, more generally, if I have a matrix A [m x n] and I want to create a matrix B also [m x n], in which each column of B should have the decile of the corresponding column in A , is there a way of doing it without a for loop through the columns?
Hope the problem at hand is clear. So far I have been doing it using the sort function and then manually assigning the deciles, but it is very inefficient.
This is how I would do it:
N = 10;
B = ceil(sum(bsxfun(#le, A(:), A(:).'))*N/numel(A));
This counts, for each element, how many elements are less than or equal to it; and then rounds the results to 10 values.
Depending on how you define deciles, you may want to change #le to #lt, or ceil to floor. For numel(A) multiple of N, the above definition gives exactly numel(A)/N values in each of the N quantiles. For example,
>> A = rand(1,8)
A =
0.4387 0.3816 0.7655 0.7952 0.1869 0.4898 0.4456 0.6463
>> N = 4;
>> B = ceil(sum(bsxfun(#le, A(:), A(:).'))*N/numel(A))
B =
2 1 4 4 1 3 2 3
I have a matrix of growing length for example a 4-by-x matrix A where x is increasing in a loop. I want to find the smallest column c where all columns before that, each, carry one single number. The matrix A can look like:
A = [1 2 3 4;
1 2 3 5;
1 2 3 1;
1 2 3 0];
where c=3, and x=4.
At each iteration of the loop where A grows in length, the value of index c grows as well. Therefore, at each iteration, I want to update the value of c. How efficiently can I code this in Matlab?
Let's say you had the matrix A and you wanted to check a particular column iito see if all its elements are the same. The code would be:
all(A(:, ii)==A(1, ii)) % checks if all elements in column are same as first element
Also, keep in mind that once the condition is broken, x cannot be updated anymore. Therefore, your code should be:
x = 0;
while true
%% expand A by one column
if ~all(A(:, x)==A(1, x)) % true if all elements in column are not the same as first element
break;
end
x = x+1;
end
You could use this:
c = find(arrayfun(#(ind)all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
This finds the first column where not all values are the same. If you run this in a loop, you can detect when entries in a column start to differ:
for x = 1:maxX
% grow A
c = find(arrayfun(#(ind)~all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
% If c is empty, all columns have values equal to first row.
% Otherwise, we have to subtract 1 to get the number of columns with equal values
if isempty(c)
c = x;
else
c = c - 1;
end
end
Let me give a try as well:
% Find the columns which's elements are same and sum the logical array up
c = sum(A(1,:) == power(prod(A,1), 1/size(A,1)))
d=size(A,2)
To find the last column such that each column up to that one consists of equal values:
c = find(any(diff(A,1,1),1),1)-1;
or
c = find(any(bsxfun(#ne, A, A(1,:)),1),1)-1;
For example:
>> A = [1 2 3 4 5 6;
1 2 3 5 5 7;
1 2 3 1 5 0;
1 2 3 0 5 8];
>> c = find(any(diff(A,1,1),1),1)-1
c =
3
You can try this (easy and fast):
Equal_test = A(1,:)==A(2,:)& A(2,:)==A(3,:)&A(3,:)==A(4,:);
c=find(Equal_test==false,1,'first')-1;
You can also check the result of find if you want.
I am comparing a 100 x 1 called "x" with the value from a 4 x 1 matrix called "xCP". I need a finalMatrix with only the values in x that are higher than the third value from xCP. However, the code i have below provides me with a 100 x 1 matrix with zeros in rows that the value are not higher. I only want the 25(e.g.) rows in finalMatrix that have the higher value. So my finalMatrix needs to be a 25 x 1 with values instead of 100 x 1 with 25 values and 75 zeros.
This is what i have so far:
K = size (x)
length = K(1)
finalMatrix = zeros(length,1);
count = 1;
for i=1:length;
if x(count,1) >= xCP(3)
finalMatrix(count,1) = x(count,1);
end
count =count+1;
end
Thank you!
You can add finalMatrix(count+1:length,1) = [] in the end of the code. It will remove the excess elements.