deleting a column in hansl - matlab

I have a very simple question. I want to delete a column from a matrix in a loop.
In Matlab I use the following:
for a certain i,
X(:,i)=[]
which deletes the column an reshapes the matrix.
I want to know the equivalent in Hansl (Gretl) program, please.
Thanks!

Sorry it's probably too late for you now, but I just saw your question and maybe it's useful for others.
In hansl (gretl's scripting and matrix language) I could think of several possibilities:
First, if you happen to know the number of columns and the value of i, the solution could use a hard-wired index vector (for i==2 and cols(X)==5 here):
X = X[, {1, 3,4,5}]
Secondly, since the first solution is probably too restrictive, you could concatenate the left and right parts of the matrix, as in:
X = X[, 1: i-1] ~ X[, i+1 :cols(X)]
But the problem here is that i must not index the first or last column, or the indexing will produce an error.
So my final suggestion that should work universally is:
X = selifc( X, ones(1, i-1) ~ 0 ~ ones(1, cols(X) - i) )
The selifc() function discards the column for which the second vector argument has a 0 entry. This also works for i==1 or i==cols(X).
A shorter variation of this final solution might be:
X = selifc(X, seq(1, cols(X)) .!= i)
which does an element-wise not-equal-to-i comparison (.!=) of the column indices constructed with the seq() function. But it's probably not as readable as the previous way.
good luck!

Related

How do I reshape a non-quadratic matrix?

I have a column vector A with dimensions (35064x1) that I want to reshape into a matrix with 720 lines and as many columns as it needs.
In MATLAB, it'd be something like this:
B = reshape(A,720,[])
in which B is my new matrix.
However, if I divide 35604 by 720, there'll be a remainder.
Ideally, MATLAB would go about filling every column with 720 values until the last column, which wouldn't have 720 values; rather, 504 values (48x720+504 = 35064).
Is there any function, as reshape, that would perform this task?
Since I am not good at coding, I'd resort to built-in functions first before going into programming.
reshape preserves the number of elements but you achieve the same in two steps
b=zeros(720*ceil(35604/720),1); b(1:35604)=a;
reshape(b,720,[])
A = rand(35064,1);
NoCols = 720;
tmp = mod(numel(A),NoCols ); % get the remainder
tmp2 = NoCols -tmp;
B = reshape([A; nan(tmp2,1)],720,[]); % reshape the extended column
This first gets the remainder after division, and then subtract that from the number of columns to find the amount of missing values. Then create an array with nan (or zeros, whichever suits your purpose best) to pad the original and then reshape. One liner:
A = rand(35064,1);
NoCols = 720;
B = reshape([A; nan(NoCols-mod(numel(A),NoCols);,1)],720,[]);
karakfa got the right idea, but some error in his code.
Fixing the errors and slightly simplifying it, you end up with:
B=nan(720,ceil(numel(a)/720));
B(1:numel(A))=A;
Create a matrix where A fits in and assingn the elemnent of A to the first numel(A) elements of the matrix.
An alternative implementation which is probably a bit faster but manipulates your variable b
%pads zeros at the end
A(720*ceil(numel(A)/720))=0;
%reshape
B=reshape(A,720,[]);

MATLAB function that gives all the positive integers in a column vector

I need to create a function that has the input argument n, a integer , n>1 , and an output argument v, which is a column vector of length n containing all the positive integers smaller than or equal to n, arranged in such a way that no element of the vector equals its own index.
I know how to define the function
This is what I tried so far but it doesn't work
function[v]=int_col(n)
[1,n] = size(n);
k=1:n;
v=n(1:n);
v=k'
end
Let's take a look at what you have:
[1,n] = size(n);
This line doesn't make a lot of sense: n is an integer, which means that size(n) will give you [1,1], you don't need that. (Also an expression like [1,n] can't be on the left hand side of an assignment.) Drop that line. It's useless.
k=1:n;
That line is pretty good, k is now a row vector of size n containing the integers from 1 to n.
v=n(1:n);
Doesn't make sense. n isn't a vector (or you can say it's a 1x1 vector) either way, indexing into it (that's what the parentheses do) doesn't make sense. Drop that line too.
v=k'
That's also a nice line. It makes a column vector v out of your row vector k. The only thing that this doesn't satisfy is the "arranged in such a way that no element of the vector equals its own index" part, since right now every element equals its own index. So now you need to find a way to either shift those elements or shuffle them around in some way that satisfies this condition and you'd be done.
Let's give a working solution. You should really look into it and see how this thing works. It's important to solve the problem in smaller steps and to know what the code is doing.
function [v] = int_col(n)
if n <= 1
error('argument must be >1')
end
v = 1:n; % generate a row-vector of 1 to n
v = v'; % make it a column vector
v = circshift(v,1); % shift all elements by 1
end
This is the result:
>> int_col(5)
ans =
5
1
2
3
4
Instead of using circshift you can do the following as well:
v = [v(end);v(1:end-1)];

MATLAB: changes in vector elements

I have a some vectors of different large sizes and their value is between 0 and 1. I want to save those indices of elements which there would be any changes in decimal. For an small example, let's assume
V=[0.02,0.1,0.4,0.0054,0.05];
Now the ouptput for this should be as
i={2,4,5}
Would you please let me know how it can be done?
As suggested by #Luis Mendo (including his suggestion with removing ~= 0), here the comment as answer. You can use the logarithm function to determine your number of decimals for you.
i = find(diff(floor(log10(V))))+1
Be sure to use floor to have integer values you can compare to 0.
count = arrayfun(#(x) regexp(num2str(x),'\.','split'),V, 'UniformOutput', false)
dp = cell2mat(arrayfun(#(x) length(x{2}),count, 'UniformOutput', false))
find(diff(dp))+1
I did it this way. First I split the numbers and next i find the length of the second term and lastly, I find whether the length is different from its previous..

MATLAB: I want to threshold a matrix, based on thresholds in a vector, without a for loop. Possible?

Let us say I have the following:
M = randn(10,20);
T = randn(1,20);
I would like to threshold each column of M, by each entry of T. For example, find all indicies of all elements of M(:,1) that are greater than T(1). Find all indicies of all elements in M(:,2) that are greater than T(2), etc etc.
Of course, I would like to do this without a for-loop. Is this possible?
You can use bsxfun like this:
I = bsxfun(#gt, M, T);
Then I will be a logcial matrix of size(M) with ones where M(:,i) > T(i).
You can use bsxfun to do things like this, but it may not be faster than a for loop (more below on this).
result = bsxfun(#gt,M,T)
This will do an element wise comparison and return you a logical matrix indicating the relationship governed by the first argument. I have posted code below to show the direct comparison, indicating that it does return what you are looking for.
%var declaration
M = randn(10,20);
T = randn(1,20);
% quick method
fastres = bsxfun(#gt,M,T);
% looping method
res = false(size(M));
for i = 1:length(T)
res(:,i) = M(:,i) > T(i);
end
% check to see if the two matrices are identical
isMatch = all(all(fastres == res))
This function is very powerful and can be used to help speed up processes, but keep in mind that it will only speed things up if there is a lot of data. There is a bit of background work that bsxfun must do, which can actually cause it to be slower.
I would only recommend using it if you have several thousand data points. Otherwise, the traditional for-loop will actually be faster. Try it out for yourself by changing the size of the M and T variables.
You can replicate the threshold vector and use matrix comparison:
s=size(M);
T2=repmat(T, s(1), 1);
M(M<T2)=0;
Indexes=find(M);

How to extract a row of a matrix

In every other language if I have a matrix, if I call a mono-dimensional index, the result will be an array.I don't know why in Matlab if you take a single index of a matrix, you'll get a single element, that's stupid.
Anyway in C:
mat[4][4];
mat[0] is an array.
In Matlab:
mat=[1 2; 3 4];
How do I take the first row of the matrix? mat(1) is 1, not [1 2].
EDIT: There is another problem, I have a problem with this function:
function str= split(string, del)
index=1;
found=0;
str=['' ; ''];
for i=1:length(string)
if string(i)==del
found=1;
index=1;
elseif found==1
str(2,index)=string(i);
index=index+1;
else
str(1,index)=string(i);
index=index+1;
end
end
end
This returns sometimes a matrix and sometimes an array.
For example if I use split('FF','.') I get 'FF' as result, but what if I want to return a matrix? I can't even choose the dimensione of the matrix, in this context a weak typed language is a big disvantage.
You have to say which columns you want. : stands for all indices in a dimension, so to take first row
mat(1,:)
It is not stupid, but useful. If you address a matrix with only one index, it implicitly gets converted to a vector. This gives you the option to use linear indices (see sub2ind).
This will extract the second row
vector = mat(2,:)
And This will extract the second column
vector = mat(:,2)
You can use
vector = mat(end,:)
To extract the last row
Hope this helps you
From Matrix Indexing in MATLAB:
When you index into the matrix A using only one subscript, MATLAB
treats A as if its elements were strung out in a long column vector,
by going down the columns consecutively
I just hope it doesn't look stupid to you anymore (along with the right answers from angainor and Marwan)