How can I calculate the average between two text files based on matrixes? - matlab

I am fairly new to using MATLAB. I have two text files that have 500 rows x 100 columns and every one of it has a random value.
For example:
1 2 3 4 5
6 7 8 9 2
4 5 6 6 8
So my question is, corresponding to the row and column, how can I calculate the average of each particular field corresponding to the row and column?
For instance, at (1,1) in text1.txt is 20 and at (1,1) in text2.txt is 40. The average is 30 and I want to display this result in an another file at (1,1).
Many thanks.

assuming the files are named 'a' and 'b':
load a
load b
c=(a+b)/2;
save('c','c','-ascii');

Related

reshape four dimensional matrix

I have some problems using reshape. I want to reshape a 4-dimensional matrix, and the fourth dimension has to become a column.
so if I have:
A(:,:,1,1) =
1 4
2 5
A(:,:,2,1) =
2 5
3 6
A(:,:,1,2) =
10 14
12 15
A(:,:,2,2) =
12 15
13 16
My reshape should be:
Columns 1 through 5
1 4 2 5 2
10 14 12 15 12
Columns 6 through 8
5 3 6
15 13 16
This should work:
reshape(permute(A,[1 4 2 3]),[2 8])
To understand this, you can go step by step. First do the following:
reshape(A,[2 8])
ans =
1 2 2 3 10 12 12 13
4 5 5 6 14 15 15 16
You observe that the columns of the reshaped matrix are taken by sliding across the second dimension in the original matrix. After second dimension is over, you slide over to third dimension and the re-iterate over second dimension (here first dimension is rows, second is column, so on...).
What you want to do is, iterate over 4th dimension (as if its a second dimension). You also want (4,14) after (1,10). You can see that corresponding elements vary across second dimension (but reshape is going to slide over third dimension, no matter what. So swap 2nd and 3rd dimensions).
Finally, you get, reshape(permute(A,[1 4 2 3]),[2 8]).
I have always had a hard time explaining permute to somebody. I hope I didn't confuse you more.
You're going to have to do a permutation first to put the dimensions in the appropriate order. Try this:
reshape(permute(A,[4,2,1,3]),[2,8])
or break it up into two separate permutations, one to switch dimensions 1 and 2 in the original array, then reshape to 8x2, then take the transpose:
reshape(permute(A,[2,1,3,4]),[8,2])'

Matlab general matrix indexing for accesing several rows

Edit for clarity:
I have two matrices, p.valor 2x1000 and p.clase 1x1000. p.valor consists of random numbers spanning from -6 to 6. p.clase contains, in order, 200 1:s, 200 2:s and 600 3:s. What I wan´t to do is
Print p.valor using a diferent color/prompt for each clase determined in p.clase, as in following figure.
I first wrote this, in order to find out which locations in p.valor represented where the 1,2 respective 3 where in p.clase
%identify the locations of all 1,2 respective 3 in p.clase
f1=find(p.clase==1);
f2=find(p.clase==2);
f3=find(p.clase==3);
%define vectors in p.valor representing the locations of 1,2,3 in p.clase
x1=p.valor(f1);
x2=p.valor(f2);
x3=p.valor(f3);
There is 200 ones (1) in p.valor, thus, is x1=(1:200). The problem is that each number one(1) (and, respectively 2 and 3) represents TWO elements in p.valor, since p.valor has 2 rows. So even though p.clase and thus x1 now only have one row, I need to include the elements in the same colums as all locations in f1.
So the different alternatives I have tried have not yet been succesfull. Examples:
plot(x1(:,1), x1(:,2),'ro')
hold on
plot(x2(:,1),x2(:,2),'k.')
hold on
plot(x3(:,1),x3(:,2),'b+')
and
y1=p.valor(201:400);
y2=p.valor(601:800);
y3=p.valor(1401:2000);
scatter(x1,y1,'k+')
hold on
scatter(x2,y1,'b.')
hold on
scatter(x3,y1,'ro')
and
y1=p.valor(201:400);
y2=p.valor(601:800);
y3=p.valor(1401:2000);
plot(x1,y1,'k+')
hold on
plot(x2,y2,'b.')
hold on
plot(x3,y3,'ro')
My figures have the axisies right, but the plotted values does not match the correct figure provided (see top of the question).
Ergo, my question is: how do I include tha values on the second row in p.valor in my plotted figure?
I hope this is clearer!
Values from both rows simultaneously can be accessed using this syntax:
X=p.value(:,findX)
In this case, resulting X matrix will be a matrix having 2 rows and length(findX) columns.
M = magic(5)
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
M2 = M(1:2, :)
M2 =
17 24 1 8 15
23 5 7 14 16
Matlab uses column major indexing. So to get to the next row, you actually just have to add 1. Adding 2 to an index on M2 here gets you to the next column, or adding 5 to an index on M
e.g. M2(3) is 24. To get to the next row you just add one i.e. M2(4) returns 5.To get to the next column add the number of rows so M2(2 + 2) gets you 1. If you add the number of columns like you suggested you just get gibberish.
So your method is very wrong. Freude's method is 100% correct, it's much easier to use subscript indexing than linear indexing for this. But I just wanted to explain why what you were trying doesn't work in Matlab. (aside from the fact that X=p.value(findX findX+1000) gives you a syntax error, I assume you meant X=p.value([findX findX+1000]))

How can I flatten every n rows in matrix using Matlab?

I can easily flatten an entire matrix into one row using reshape(M,1,[]). However, this time I want to flatten every n rows into one row. Thus, if we start with 100 rows and n=10, we will end up with 10 rows.
e.g.
1 2 3
4 5 6
7 8 9
10 11 12
with n=2 changes into
1 2 3 4 5 6
7 8 9 10 11 12
Is there a simple way to do this?
Suppose your original matrix is m, then:
reshape(m',[6 2])'
produces the required output. I'll leave it to you to generalise to other cases; comment or post again if that causes you problems.
This should work.
reshape(M',l/n,n)'
Where n is what you've defined and l is the total elements in M.
EDIT: Made it one-liner

Removing rows with identical first column value in matlab

I have a cell matrix of size 10000 X 3 in Matlab and I would like to remove rows with the same value in the first column.
That is, if row i and row j have the same value in the first column, I'd like to delete both rows.
I should also say that there can be more than two rows with the same value in the first column and in that case, I'd like to delete all these rows.
How do I do it?
Thanks!
You can use the functions histc, unique and logical indexing to achieve what you want. Here's a small example.
a=randi(10,5,3) %#generate a sample random matrix
a =
5 3 5
5 7 10
7 7 4
8 2 6
8 2 3
[uniqVals,uniqIndx]=unique(a(:,1)); %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)
b=a(uniqIndx(count==1),:)
b =
7 7 4
Only the row with the non-repeated element is selected. Since you said that you have a cell matrix, simply covert it to a matrix using cell2mat before doing this.

how to find the corresponding point in other row of an array in matlab?

i have an array lets say
x =
1 2 2
5 8 7
now i want to get the corresponding values of a number in 2nd row.
like i have
number =2
and i want
ans= 8 and 7
as 8 and 7 are the corresponding points for 2.
X(2,X(1,:)==2)
should do. Hope you can figure out how.