I have a testfile.txt, which is a 4 x 4 matrix and tab delimited
1 1 3 4
2 2 3 4
3 1 3 4
4 2 3 4
The output i want is like so:
If it detects that the second column has a 1, insert a new column on the right side, and the new column should contains something like x=[1 1 0 3]
If it detects that the second column has a 2, insert a new column on the right side, and the new column should contains something like y=[2 3 4 5]
This is how the output should look like:
1 1 x=[1 1 0 3] 3 4
2 2 y=[2 3 4 5] 3 4
3 1 x=[1 1 0 3] 3 4
4 2 y=[2 3 4 5] 3 4
Ultimately, in MATLAB this is the output I want to get:
1 1 1 1 0 3 3 4
2 2 2 3 4 5 3 4
3 1 1 1 0 3 3 4
4 2 2 3 4 5 3 4
What I've tried is:
test=dlmread('testfile.txt','\t');
m=length(test);
for i=1:m
if find(test(:,2)==1)>0
x=[1 1 0 3];
test=[test(:,1) x test(:,3:4)];
elseif find(test(:,2)==2)>0
y=[2 3 4 5];
test=[test(:,1) y test(:,3:4)];
dlmwrite('testfile.txt',test,'delimiter','\t','precision','%.4f');
end
end
The error I get is the following:
Dimensions of matrices being concatenated are not consistent.
The error is from the following statement:
Error in : test=[test(:,1) x test(:,3:4)]
I'll be really appreciative if someone can help me, since i'm quite new in MATLAB.
Thanks in advance!
Here is a completely vectorized solution for you. Let's go through this one step at a time. You obviously are reading in the text data right, so let's keep that code the same.
test = dlmread('testfile.txt','\t');
What I'm going to do is create a 2D array where the first row corresponds to your x that you want to insert, while the second row corresponds to the y that you want to insert. In other words, create a variable called insertData such that:
insertData = [1 1 0 3; 2 3 4 5];
Next, you simply have to use the second column to figure out which row of data from insertData you want to insert into your final matrix. You can then use this to create your final matrix, which we will store in testOut. In other words:
testOut = [test(:,1:2) insertData(test(:,2),:) test(:,3:4)]
The output I get is:
testOut =
1 1 1 1 0 3 3 4
2 2 2 3 4 5 3 4
3 1 1 1 0 3 3 4
4 2 2 3 4 5 3 4
Let's walk through the above code slowly. The first two columns of your data stored in test and the last two columns in your data stored in test are the same. You want to insert the data right in the middle. As such, you create a new matrix called testOut where the first two columns are the same, and then in the middle is where it gets interesting. Every time the second column has a 1, we access the first row of insertData, and we place our data in the corresponding row. Every time the second column has a 2, we access the second row of insertData, and we place our data in the corresponding row. To finish everything off, the last two columns should be the same.
Minor Note
If you want to understand why your code isn't working, it's because you are not concatenating the rows properly. In addition, in your for loop, you are using : to access all of the rows for a particular column when you should be accessing one row at a time... at least that's how I'm interpreting your for loop. This change must also be done in your if statements. Also, you are adding onto the test variable, when you need to declare a NEW variable. Also, you need to move the dlmwrite method so that it is called AFTER the for loop has finished and you have finished creating the new matrix. The combination of all of these things is ultimately why you are getting errors in your code.
Basically, what you need to do, if you want to use your code, is do this:
test=dlmread('testfile.txt','\t');
m=length(test);
testOut = []; %// Must declare NEW variable
for i=1:m
if find(test(i,2)==1)>0 %// Change
x=[1 1 0 3];
testOut=[testOut; test(i,1) x test(i,3:4)]; %// NEW
elseif find(test(i,2)==2)>0 %// Change
y=[2 3 4 5];
testOut=[testOut; test(i,1) y test(i,3:4)]; %// NEW
end
end
%// Move this out!
dlmwrite('testfile.txt',testOut,'delimiter','\t','precision','%.4f');
Take a look at how testOut is being concatenated in the for loop. You are going to take the current state of testOut, move to the next row using ; then add your new data in.
This code should now work, but you can easily achieve what you want to do in just two lines.
Hope this helped!
Related
Suppose that I have a matrix , let's call it A, as follows:
1 2 3 4 5 1 2 3 4 5
0 2 4 6 8 1 3 5 7 9
And I want to reshape it into a matrix like this:
1 2 3 4 5
0 2 4 6 8
1 2 3 4 5
1 3 5 7 9
So, basically, what I want to be done is that MATLAB first reads a block of size (2,5) and then splits the remaining matrix to the next row and then repeats this so on so forth until we get something like in my example.
I tried to do this using MATLAB's reshape command in several ways but I failed. Any help is appreciated. In case that it matters, my original data is larger. It's (2,1080). Thanks.
I don't believe you can do this in a single command, but perhaps someone will correct me. If speed isn't a huge concern a for loop should work fine.
Alternatively you can get your results by reshaping each row of A and then placing the results into every other row of a new matrix. This will also work with your larger data.
A = [1 2 3 4 5 1 2 3 4 5
0 2 4 6 8 1 3 5 7 9];
An = zeros(numel(A)/5, 5); % Set up new, empty matrix
An(1:2:end,:) = reshape(A(1,:), 5, [])'; % Write the first row of A to every other row of An
An(2:2:end,:) = reshape(A(2,:), 5, [])' % Write second row of A to remaining rows
An =
1 2 3 4 5
0 2 4 6 8
1 2 3 4 5
1 3 5 7 9
You may need to read more about indexing in the Matlab's documentation.
For your example, it is easy to do the following
A=[1 2 3 4 5 1 2 3 4 5; 0 2 4 6 8 1 3 5 7 9]
a1=A(:,1:5); % extract all rows, and columns from 1 to 5
a2=A(:,6:end); % extract all rows, and columns from 6 to end
B=[a1;a2] % construct a new matrix.
It is not difficult to build some sort of loops to extract the rest.
Here's a way you can do it in one line using the reshape and permute commands:
B = reshape(permute(reshape(A,2,5,[]), [1,3,2]), [], 5);
The reshape(A,2,5,[]) command reshapes your A matrix into a three-dimensional tensor of dimension 2 x 5 x nblocks, where nblocks is the number of blocks in A in the horizontal direction. The permute command then swaps the 2nd and 3rd dimensions of this 3D tensor, so that it becomes a 2 x nblocks x 5 tensor. The final reshape command then transforms the 3D tensor into a matrix of dimension (2*nblocks) x 5.
Looking at the results at each stage may give you a better idea of what's happening.
I am aware of MATLAB's datasample which allows to select k times from a certain population. Suppose population=[1,2,3,4] and I want to uniformly sample, with replacement, k=5 times from it. Then:
datasample(population,k)
ans =
1 3 2 4 1
Now, I want to repeat the above experiment N=10000 times without using a for loop. I tried doing:
datasample(repmat(population,N,1),5,2)
But the output I get is (just a short excerpt below):
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
1 3 2 1 3
Every row (result of an experiment) is the same! But obviously they should be different... It's as though some random seed is not updating between rows. How can I fix this? Or some other method I could use that avoids a for loop? Thanks!
You seem to be confusing the way datasample works. If you read the documentation on the function, if you specify a matrix, it will generate a data sampling from a selection of rows in the matrix. Therefore, if you simply repeat the population vector 10000 times, and when you specify the second parameter of the function - which in this case is how many rows of the matrix to extract, even though the actual row locations themselves are different, the actual rows over all of the matrix is going to be the same which is why you are getting that "error".
As such, I wouldn't use datasample here if it is your intention to avoid looping. You can use datasample, but you'd have to loop over each call and you explicitly said that this is not what you want.
What I would recommend you do is first create your population vector to have whatever you desire in it, then generate a random index matrix where each value is between 1 up to as many elements as there are in population. This matrix is in such a way where the number of columns is the number of samples and the number of rows is the number of trials. Once you create this matrix, simply use this to index into your vector to achieve the desired sampling matrix. To generate this random index matrix, randi is a fine choice.
Something like this comes to mind:
N = 10000; %// Number of trials
M = 5; %// Number of samples per trial
population = 1:4; %// Population vector
%// Generate random indices
ind = randi(numel(population), N, M);
%// Get the stuff
out = population(ind);
Here's the first 10 rows of the output:
>> out(1:10,:)
ans =
4 3 1 4 2
4 4 1 3 4
3 2 2 2 3
1 4 2 2 2
1 2 3 4 2
2 2 3 2 1
4 1 3 2 4
1 4 1 3 1
1 1 2 4 4
1 2 4 2 1
I think the above does what you want. Also keep in mind that the above code generalizes to any population vector you want. You simply have to change the vector and it will work as advertised.
datasample interprets each column of your data as one element of your population, sampling among all columns.
To fix this you could call datasample N times in a loop, instead I would use randi
population(randi(numel(population),N,5))
assuming your population is always 1:p, you could simplify to:
randi(p,N,5)
Ok so both of the current answers both say don't use datasample and use randi instead. However, I have a solution for you with datasample and arrayfun.
>> population = [1 2 3 4];
>> k = 5; % Number of samples
>> n = 1000; % Number of times to execute datasample(population, k)
>> s = arrayfun(#(k) datasample(population, k), n*ones(k, 1), 'UniformOutput', false);
>> s = cell2mat(s);
s =
1 4 1 4 4
4 1 2 2 4
2 4 1 2 1
1 4 3 3 1
4 3 2 3 2
We need to make sure to use 'UniformOutput', false with arrayfun as there is more than one output. The cell2mat call is needed as the result of arrayfun is a cell array.
I Have a matrix in matlab,for example
A=[1 2 3
1 3 5
1 4 2
2 3 1
2 4 3]
and an array like this:
b=[3
4]
now I want to find rows in A, that the second column equals one of the values in b. In this example:
Result=[1 3 5
1 4 2
2 3 1
2 4 3]
I want to find this without using loop.
That's easy: use the ismember function:
Result = A(ismember(A(:,2),b),:);
You could also use bsxfun:
Result = A(any(bsxfun(#eq, A(:,2).', b(:)),1),:)
I have a 2x100 matrix. It contains 100 elements from 2 different classes. So each element consists of the value itself and the label with the class it belongs to(1 or 2). I want to mix this data into another 2x100 matrix, where the values stay still connected to their labels.
An example with a 2x5 matrix would be:
A=[1 2 3 4 5;
1 2 2 2 1]
After mixing:
A=[2 3 5 1 4;
2 2 1 1 2]
How can I do this? Thanks!
You can index the entire columns (and randomly change the order using randperm)
Amix = A( :, randperm(size(A,2)) );
See an example at ideone.
How do I change the list of value to all 1? I need the top right to bottom left also end up with 1.
rc = input('Please enter a value for rc: ');
mat = ones(rc,rc);
for i = 1:rc
for j = 1:rc
mat(i,j) = (i-1)+(j-1);
end
end
final = mat
final(diag(final)) = 1 % this won't work?
Code for the original problem -
final(1:size(final,1)+1:end)=1
Explanation: As an example consider a 5x5 final matrix, the diagonal elements would have indices as (1,1), (2,2) .. (5,5). Convert these to linear indices - 1, 7 and so on till the very last element, which is exactly what 1:size(final,1)+1:end gets us.
Edit : If you would like to set the diagonal(from top right to bottom left elements) as 1, one approach would be -
final(fliplr(eye(size(final)))==1)=1
Explanation: In this case as well we can use linear indexing, but just for more readability and maybe a little fun, we can use logical indexing with a proper mask, which is being created with fliplr(eye(size(final)))==1.
But, if you care about performance, you can use linear indexing here as well, like this -
final(sub2ind(size(final),1:size(final,1),size(final,2):-1:1))=1
Explanation: Here we are creating the linear indices with the rows and columns indices of the elements to be set. The rows here would be - 1:size(final,1) and columns are size(final,2):-1:1. We feed these two to sub2ind to get us the linear indices that we can use to index into final and set them to 1.
If you would to squeeze out the max performance here, go with this raw version of sub2ind -
final([size(final,2)-1:-1:0]*size(final,1) + [1:size(final,1)])=1
All of the approaches specified so far are great methods for doing what you're asking.
However, I'd like to provide another viewpoint and something that I've noticed in your code, as well as an interesting property of this matrix that may or may not have been noticed. All of the anti-diagonal values in your matrix have values equal to rc - 1.
As such, if you want to set all of the anti-diagonal values to 1, you can cheat and simply find those values equal to rc-1 and set these to 1. In other words:
final(final == rc-1) = 1;
Minor note on efficiency
As a means of efficiency, you can do the same thing your two for loops are doing when constructing mat by using the hankel command:
mat = hankel(0:rc-1,rc-1:2*(rc-1))
How hankel works in this case is that the first row of the matrix is specified by the vector of 0:rc-1. After, each row that follows incrementally shifts values to the left and adds an increasing value of 1 to the right. This keeps going until you encounter the vector seen in the second argument, and at this point we stop. In other words, if we did:
mat = hankel(0:3,3:6)
This is what we get:
mat =
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Therefore, by specifying rc = 5, this is the matrix I get with hankel, which is identical to what your code produces (before setting the anti-diagonal to 1):
mat =
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
Tying it all together
With hankel and the cheat that I mentioned, we can compute what you are asking in three lines of code - with the first line of code asking for the dimensions of the matrix:
rc = input('Please enter a value for rc: ');
mat = hankel(0:rc-1, rc-1:2*(rc-1));
mat(mat == rc-1) = 1;
mat contains your final matrix. Therefore, with rc = 5, this is the matrix I get:
mat =
0 1 2 3 1
1 2 3 1 5
2 3 1 5 6
3 1 5 6 7
1 5 6 7 8
Here's a simple method where I just add/subtract the appropriate matrices to end up with the right thing:
final=mat-diag(diag(mat-1))+fliplr(diag([2-rc zeros(1,rc-2) 2-rc]))
Here is one way to do it:
Say we have a the square matrix:
a = ones(5, 5)*5
a =
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
You can remove the diagonal, then create a diagonal list of ones to replace it:
a = a - fliplr(diag(diag(fliplr(a)))) + fliplr(diag(ones(length(a), 1)))
a =
5 5 5 5 1
5 5 5 1 5
5 5 1 5 5
5 1 5 5 5
1 5 5 5 5
The diag(ones(length(a), 1)) can be any vector, ie. 1->5:
a = a - fliplr(diag(diag(fliplr(a)))) + fliplr(diag(1:length(a)))
a =
5 5 5 5 1
5 5 5 2 5
5 5 3 5 5
5 4 5 5 5
5 5 5 5 5