tesconf is 3x3 matrix;
tes_avg = (diag(tesconf)./sum(tesconf,2));
example that the result of tes_avg is given [0.345;0.3423;0.483]
However, i wish to get average result of this 3 values, how should i change the code above?Please advise...
Simply avg = mean(tes_avg); (or directly tes_avg = mean(diag(tesconf)./sum(tesconf,2));).
Related
I'm using the "ComplexHeatmap" package to create a heatmap of the correlations in a matrix.
I want to use my own clustering for the dendrogram of the heatmap so I run the code below:
library(ComplexHeatmap);
mat = matrix(rnorm(800),80,10);
cor.mat= cor(mat)
dist.mat = (1-cor.mat)/2;
rowdist = dist(as.matrix(dist.mat), method = "euclidean")
rowcluster = hclust(rowdist, method = "ward.D2")
coldist = dist(t(as.matrix(dist.mat)), method = "euclidean")
colcluster = hclust(coldist, method = "ward.D2")
par(mfrow=c(1,2));plot(rowcluster);plot(colcluster);
Heatmap(cor.mat ,cluster_rows=rowcluster, cluster_columns=colcluster)
Problem is, I get different clustering on the rows and columns (asymmetrical), despite the fact that the cluster objects are the same.
Even if I pass the Heatmap function the exact same object for rows and columns it still displays a different order for rows and columns.
if I just create the dendrograms i.e. plot(rowcluster) or plot(colcluster) they are identical.
I want to get a symmetrical heatmap.
Any idea why this happens?
Thanks
Use rowclust=colclust.
No need to transpose.
But note that you have a distance matrix already, so "euclidean" is wrong. You are computing a distance matrix of your distance matrix!
I have a matrix called "featureMatrix". using size(featureMatrix) the result is: 11843 720. I want to shuffle the content of this matrix using randperm. First I choose the seed using rng(1). Then I use randperm: featureMatrixRnd = featureMatrix(randperm(length(featureMatrix))');. But it didn't work good. In fact if i write size(featureMatrixRnd) I obtain 11843 1, instead of 11843 720. why?
Because you used length, which selects the longest dimension. First use numel instead of length to get all elements, then reshape back to your original size:
OrgSize = size(featureMatrix);
featureMatrixRnd = randperm(numel(featureMatrix));
out = reshape(featureMatrix(featureMatrixRnd),OrgSize);
This answer is similar to Adriaan's, but does not require a reshape:
featureMatrix(:) = featureMatrix(randperm(numel(featureMatrix)));
This is the best solution:
rng(1);
idx = randperm(size(featureMatrix,1));
outfeatureMatrixRnd = featureMatrix(idx,:);
Using
OrgSize = size(featureMatrix);
featureMatrixRnd = randperm(numel(featureMatrix));
out = reshape(featureMatrix(featureMatrixRnd),OrgSize);
is not good, because I don't need to shuffle everythink independently
I have two matrices of results, A = 128x631 and B = 128x1014 and I have a function SSD that takes two elements (x,y) as parameters and then calculates the sum of squared differences. I also have a 631x1014 matrix of 0s, called SSDMatrix, ready to put the results of my SSD function into.
What I'm trying to do is compare each element of A with each element of B by passing them into SSD, but I can't figure out how to structure my for loops to get the desired results.
When I try:
SSDMatrix = SSD(A, B);
I get exactly the result I'm looking for, but only for the first cell. How can I repeat this process for each element of A and B?
Currently I have this:
SSDMatrix = zeros(NumFeatures1,NumFeatures2);
for i = 1:631
for j = 1:1014
SSDMatrix(i,j) = SSD(A,B);
end
end
This just results in the first answer being repeated 631*1014 times, so I need a way to index A and B to get the appropriate answer for each (i,j) of SSDMatrix.
It seems you were needed to do something like this -
SSDMatrix = zeros(NumFeatures1,NumFeatures2);
for i = 1:631
for j = 1:1014
SSDMatrix(i,j) = sum( (A(:,i) - B(:,j)).^ 2 );
end
end
This, you can achieve with pdist2 as well that gets us the square root of summed squared distances. Now, please do note that pdist2 is part of the Statistics Toolbox. So, to get the desired output, you can do -
out = pdist2(A.',B.').^2;
Or with bsxfun -
out = squeeze(sum(bsxfun(#minus,A,permute(B,[1 3 2])).^2,1));
I have a matrix 64x64x32x90 which stands for pixels at x,y,z, at time t.
I have a reference signal 1x90 which stands for the behavior I expect for a pixel at some point (x,y,z).
I am constructing a new image of the correlation between each pixel versus my reference.
load('DATA.mat');
ON = ones(1,10);
OFF = zeros(1,10);
taskRef = [OFF ON OFF ON OFF ON OFF ON OFF];
corrImage = zeros(64,64,36);
for i=1:64,
for j=1:63,
for k=1:36
signal = squeeze(DATA(i,j,k,:));
coef = corrcoef(signal',taskRef);
corrImage(i,j,k) = coef(2);
end
end
end
My process is too slow. Is there a way to get rid of my loops or adjust the code to have a better runtime?
Reshape your data so that its first three dimensions are collapsed into one (so now there are 64*64*32 rows and 90 columns).
Then use pdist2 (with 'correlation' option) to compute the correlation of each row with the expected pattern.
Finally, reshape result into the desired shape.
DATA2 = reshape(DATA, [],90);
corrImage = 1 - pdist2(DATA2, taskRef, 'correlation');
corrImage = reshape(corrImage, 64,64,32);
I have a 3-dimensial matrix W of size 160x170x18 and I want to compute the difference
between each sucessive matrices inside W.
For example diff1 = W(:,:,1) - W(:,:,2) and diff2 = W(:,:,2) - W(:,:,3), etc ...
Next I want to select some special parts of the resulting matrices, For example:
NewDiff1 = [diff1(20:50,110:140); diff1(60:90,110:140)];
and the same thing for the other matrices.
finally I want to compute the mean of each matrix and the error as follow:
mean1 = mean(mean(NewDiff1));
er1 = 0.1-abs(mean1);
I succeeded to do this for each matrix alone, but prefer to do all at once in a for loop.
The expression
diff1 = diff(W,1,3)
will return, in your example, a 160*170*17 matrix where diffW(:,:,1) = W(:,:,2) - W(:,:,1), which isn't quite what you want. But
diff1 = (-1)*diff(W,1,3)
does, if my arithmetic is good, give you the differences you want. From there on you need something like:
newdiff1 = [diff1(20:50,110:140,:);diff1(60:90,110:140,:)];
and
means = mean(mean(newdiff1));
er1 = 0.1 - abs(mean1);
I haven't tested this thoroughly on matrices of the size you are working with, but it seems to work OK on smaller tests.
Store your matrices into a cell array and then just loop through the contents of the cell array and apply the same differencing logic to each thing. Be careful to use the {} syntax with a cell array to get its contents, rather than () which gives you the cell at a particular location.