i just started with matlab and stuck somewhere...consider example
X=(3:7)
Z=(2:6)
for (i=1:5)
y=abs(X(i)-Z);
dm=min(y);
D=find(y==min(y))
D1=Z(D);
end
i want D and D1 to be a column/row vector.Please help.
Currently you are storing scalar values into D and D1.
Maybe you wanted to save the values into i-th column of D and D1?
X=(3:7)
Z=(2:6)
for (i=1:5)
y=abs(X(i)-Z);
dm=min(y);
D(i)=find(y==min(y));
D1(i)=Z(D(i));
end
If you're looking to simply convert D and D1 from row vectors to column vectors, you can simply add the following lines at the end of your code:
D = D';
D1 = D1';
The ' operation simply gives you the transpose of the matrix in question.
use:
if isrow(D)
D = D.'; % .' is the transpose operator
end
BTW: you don't need to use parentheses that often.
I think the following will help too:
% convert ANY array into a column vector
D = D(:);
% convert ANY array into a row vector
D1 = D1(:).';
doing it like this will guarantee that one is column and the other row, without any performance loss.
Related
So I want to concatenate an m x n matrix to obtain a 1 x mn matrix. The matrix I want to concatenate are generated from a while loop. Although the number of columns will always be 3, I however cannot tell how many rows there will be for each iteration. Also, the row sizes for each iteration may not always be the same.
The code runs in cases where the row sizes were all equal to 6, but in cases where they aren't equal I get an error:
Error using vertcat Dimensions of matrices being concatenated are not consistent.
parts of the code are as follows:
A = [];
B = [];
searchArea = 2;
for ii = 1: numel(velocity)
Do ....
while area(ii,:) < searchArea
Do ....
% COLLATE vectors for A
A = [A; [Ax(ii), Ay(ii), Az(ii)]];
Do ...
end
%# Copy the A into new variable (B) and Reshape into row vector so as to associate each row to its corresponding velocity
B = [B; reshape(A.',1,[])];
A = [];
end
Could someone please advice me on what I am doing wrong here. I would clarify further if there be need. Thanks guys!
If it's your intent that B ends up being a row vector, then you need to change this:
B = [B; reshape(A.',1,[])]; % Does vertical concatenation
to this:
B = [B reshape(A.',1,[])]; % Does horizontal concatenation (note there's no semicolon)
so that each row vector gotten from reshaping A gets added to the end of the row instead of as a new row (as the semicolon indicates).
I have a A matrix of size MxN where M is large and N is around 30.
[A,B,C,...,AD] = A(:,1:30)
The reason I am asking that is that I would like to give the columns a specific name (here A,B a,c,...,AD) and not being force to write:
[A,B,C,...,AD] = deal(A(:,1),A(:,2),A(:,3),...,A(:,30))
It's usually better to keep all columns together in the matrix and just access them through their column index.
Anyway, if you really need to separate them into variables, you can convert the matrix to a cell array of its columns with num2cell, and then generate a comma-separated list to be used in the right-hand side of the assignment. Note also that in recent Matlab versions you can remove deal:
A = magic(3); % example matrix
Ac = num2cell(A, 1);
[c1 c2 c3] = Ac{:}; % or [c1 c2 c3] = deal(Ac{:});
For generating that lexicographical sequence I recently, out of ignorance, wrote this
Data = rand(2,671);
r = rem(size(Data,2),26);
m = floor(size(Data,2)/26);
Alf = char('A'+(0:25)'); %TeX-like char seq
if m == 0
zzz = Alf(1:r);
else
zzz = Alf;
for x = 1:m-1
zzz = char(zzz,[char(Alf(x)*ones(26,1)),Alf]);
end
if r > 0
zzz = char(zzz, [char(Alf(m+1)*ones(r,1)),Alf(1:r)] );
end
end
Depending on the number of columns it generates column names until ZZ. Please let me know if there is a readily made command for this in matlab.
You would never ever use eval for such things!!! eval use is dangerous and wrong (but you can't resist):
% ==========
% Assign Data to indices
% ==========
for ind = 1:size(Data,2)
eval([zzz(ind,:) '= Data(:,' num2str(ind) ');']);
end
and your workspace looks like an alphabet soup.
I need some help to vectorize the following operation since I'm a little confused.
So, I have a m-by-2 matrix A and n-by-1 vector b. I want to create a n-by-1 vector c whose entries should be the values of the second column of A whose line is given by the line where the correspondent value of b would fall...
Not sure if I was clear enough. Anyway, the code below does compute c correctly so you can understand what is my desired output. However, I want to vectorize this function since my real n and m are in the order of many thousands.
Note that values of bare non-integer and not necessarily equal to any of those in the first column of A (these ones could be non-integers too!).
m = 5; n = 10;
A = [(0:m-1)*1.1;rand(1,m)]'
b = (m-1)*rand(n,1)
[bincounts, ind] = histc(b,A(:,1))
for i = 1:n
c(i) = A(ind(i),2);
end
All you need is:
c = A(ind,2);
let us consider following bit of code:
[m,n]=size(X);
if m == (n+1)
Z = vertcat(U(:,1:2:d), V(:,1:2:d));
else
Z = vertcat(U(:,[1:2:d]));
end
C=Z(:);
What I want it to do is concatenate the singular vectors into one column vector. For example, I want to concatenate the first d left and right singular vectors, but the problem is that it creates a multidimensional column, that's why I wrote C=Z(:). But, can I use vertcat to just create a 1D column vector? Thanks in advance!
It looks like you may have just flipped your row and column indexing. U(:,1:2:d) will return a row vector, which you are then vertcating with another row vector. Try this instead:
[m,n]=size(X);
if m == (n+1)
Z = vertcat(U(1:2:d,:), V(1:2:d,:));
else
Z = vertcat(U([1:2:d]), :));
end
C=Z(:);
I hope that helps.
please help me to understand this code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
i can't find it in my book. thanks.
The code you posted can be made more readable using temporary variables:
a = (i-1)*j+1;
b = i*j;
val = y(:,a:b);
x(:,i) = mean( val, 2 )'; %# =mean( val' )
What exactly you do not understand? For meaning of mean , : and ' consult matlab help.
It would help if you said exactly what you don't understand, but here are a few tips:
if you have something like a(r,c), that means matrix a, row r, column c (always in this order). In other words, you should have two elements inside the brackets separated by a comma where the first represents the row, the second the column.
If you have : by itself in one of the sides of the comma, that means "all". Thus, if you had a(r,:), then you would have matrix a, row r, all columns.
If : is not alone in one of the sides of the comma, then it will mean "to". So if you have a(r, z:y), that means matrix a, row r, columns z to y.
Mean = average. The format of the function in Matlab is M = mean(A,dim). A will be the matrix you take the average (or mean) of, M will be the place where the results are going to go. If dim = 1, you will get a row vector with each element being the average of a column. If dim = 2 (as it is in your case), then you should get a column vector, with each element being the average of a row. Be careful, though, because at the end of your code you have ', which means transpose. That means that your column vector will be transformed into a row vector.
OK, so your code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
Start with the bit inside, that is
y(:,((i-1)*j+1):i*j)
So that is saying
matrix y(r,c)
where
r (row) is :, that is, all rows
c (column) is ((i-1)j+1):ij, that is, columns going from (i-1)j+1 until ij
Your code will then get the matrix resulting from that, which I called y(r,c), and will do the following:
mean( (y(r,c), 2 )
so get the result from above and take the mean (average) of each row. As your code has the ' afterwards, that is, you have:
mean( (y(r,c), 2 )'
then it will get the column vector and transform into a row vector. Each element of this row will be the average of a row of y(r,c).
Finally:
x(:,i) = mean( (y(r,c), 2 )';
means that the result of the above will be put in column i of matrix x.
Shouldn't this be x(i,:) instead?
The i-th column of the array x is the average of the i-th group of j columns of the array y.
For example, if i is 1 and j is 3, the 1st column of x is the average of the first three columns of y.