Error in Matrix calculation

2 Ansichten (letzte 30 Tage)
Vesp
Vesp am 22 Jun. 2017
Bearbeitet: Vesp am 23 Jun. 2017
Hello I have been trying to debug this code, but am getting incorrect values in the final steps of the code. What this code does: 1) calculate the magnitude IVT (from x1 and y1) and then mask based on shape matrix 2) Sort IVT in descending order 3) Based on the sorting of IVT (index) sort x1 and y1 4) After x1 and y1 are sorted like that of IVT take the top N values at each element Note: the top N values are defined in the count matrix 5)To check do the same for sorted_ivt 6) To check calculate magnitude of ivtx1 and ivty1 and compare to totivt
count=[NaN 15 1; 2 NaN 2;NaN 4 3];
x1=randi(200,3,3,20);
y1=randi(200,3,3,20);
shape=randi(10,3,3,20);
shape(shape==5)=NaN;
%calcualte the magnitude
ivt1=sqrt(x1.^2+y1.^2);
ivt1(isnan(shape))=0; %mask out
x1(isnan(shape))=0;
y1(isnan(shape))=0;
%sort IVT in descending order
[ivt_sorted,idx]=sort(ivt1,3,'descend');
%sort ivtx and ivty like that of ivt magnitude
[m,n,o] = size(ivt1);
ivtx1 = x1((idx-1)*m*n + reshape(1:m*n,m,[]));
[m,n,o] = size(ivt1);
ivty1 = y1((idx-1)*m*n + reshape(1:m*n,m,[]));
check=sqrt(ivtx1.^2+ivty1.^2);
isequal(ivt_sorted,check) %up to here they are the same!!!!!
%take the top N values based on count from ivtx, ivty, and ivt magnitude
[m,n,k] = size(ivtx1);
count(isnan(count)) = 0;
out_ivt1= permute(reshape((1:k)'*ones(1,m*n) <= count(:)',[],m,n),[2,3,1]).*ivtx1;
out_ivt1(out_ivt1==0)=NaN;
Finalivtx=nanmean(out_ivt1(:,:,:),3);
[m,n,k] = size(ivty1);
count(isnan(count)) = 0;
out_ivt2= permute(reshape((1:k)'*ones(1,m*n) <= count(:)',[],m,n),[2,3,1]).*ivty1;
out_ivt2(out_ivt2==0)=NaN;
Finalivty=nanmean(out_ivt2(:,:,:),3);
[m,n,k] = size(ivt1);
count(isnan(count)) = 0;
check1= permute(reshape((1:k)'*ones(1,m*n) <= count(:)',[],m,n),[2,3,1]).*ivt1;
check1(check1==0)=NaN;
topivt=nanmean(check1(:,:,:),3);
test=sqrt(Topivtx.^2+Topivty.^2);
isequal(test,topivt) % THESE ARE NOT THE SAME!!!
%

Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by