Question

I have a text file D with 98 rows and 2 columns. looks like this:

10 0.261344
11 0.456167
12 0.668595
2 0.481754
... etc

I have another excel file M with 17 rows and 2 col.
I want to subtract all rows values from the 1st row, then from 2, etc.so i get something like this:

10-11 -0.194823
10-12 -0.407251
... etc

in other words difference between 1st row and all rest, 2nd row and all rest ..excluding those rows of which 1st column has same values as in 2nd M excel file 1st column. which makes in total difference compare to 81 rows.

i have been trying to use this:

M = xlsread('...');    

FSumID=fopen(D);
 MatrixSub = loadD);
 m = textscan(FSumID,'%d %f');
horzcat(m{:}) 

NewData = M(:,1)
D_size=size(D);

for i=1:D_size

 if (ptvBodyDist(:,1)~=NewData(i))
%for calculating 
 DVec = bsxfun(@minus, ((m{i,2})), (m{i,2}));

%when i check this it gives only one loop all zeros(98,1)while i need (((98-17)^2,1). I need to change,last part of expression to get what i want but I'm stuck with bsxfun. i dont know how to make correct expression to subtract all rows.

I'm newbie in Matlab any help would be much appreciated. also if you could suggest some manuals or tutorials,apart from mathworks help. Many thanks.

Was it helpful?

Solution

Let D be n by 2 matrix (data from text file).
Let M by m by 2 matrix (data from excel file).

The first stage is to find the relevant n-m rows of D: rows that their first value does not appear in M, using setdiff.

[~, idx] = setdiff( D( :, 1 ), M( :, 1 ), 'stable' );

Now we can compute the all-vs-all diff using bsxfun

d = bsxfun( @minus, permute( D( idx, : ), [1 3 2] ), ...
                    permute( D( idx, : ), [3 1 2] ) );

Now d is |idx|x|idx|x2 array where

d( ii, jj, : ) = D( idx(ii), :) - D( idx(jj), : )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top