I should add, that creating M with the Matlab version of my routine causes absolutely no such problem.
Bug when using min() with sparse matrices?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ingo
am 8 Jan. 2012
Bearbeitet: James Tursa
am 28 Jan. 2024
Hi,
I translated one of my Matlab routines to C and now I've encountered a really strange behaviour which seems to be a bug. M is a sparse matrix and I printed M, M', min(M, M') and min(full(M), full(M')), but all of them as full matrices to see the problem better:
The last matrix is what I want and what I'm supposed to already get by using min(M, M'), but for some reason min(M, M') shows some really weird behaviour. Any gueses what causes this?
Edit: Here's the mex file:
#include "mex.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *tempArray, *outputDist[1], *inputDist[2], *outputSort[2];
double *tempVal, *pr, *sr, *sorted, *order;
mwSize n, nzmax;
mwIndex *irs, *jcs, i, j, k;
// distance function call
tempArray = mxCreateDoubleMatrix(1, 1, mxREAL);
tempVal = mxGetPr(tempArray);
inputDist[0] = prhs[0];
inputDist[1] = tempArray;
// read input matrix size
n = mxGetN(prhs[0]);
// read real part of input matrix
pr = mxGetPr(prhs[0]);
// read parameter for k
k = (mwIndex)*mxGetPr(prhs[1]);
// create output sparse matrix
nzmax = (mwSize)(n*k);
plhs[0] = mxCreateSparse(n, n, nzmax, mxREAL);
sr = mxGetPr(plhs[0]);
irs = mxGetIr(plhs[0]);
jcs = mxGetJc(plhs[0]);
// go through data points
for (i = 0; i < n; i++)
{
// calculate distance vector
tempVal[0] = (double)(i+1);
mexCallMATLAB(1, outputDist, 2, inputDist, "distEuclidean2");
// sort vector
mexCallMATLAB(2, outputSort, 1, &outputDist[0], "sort");
sorted = mxGetPr(outputSort[0]);
order = mxGetPr(outputSort[1]);
// assign values to sparse matrix
jcs[i] = i*k;
for (j = 0; j < k; j++)
{
irs[i*k+j] = (mwIndex)(order[j]-1);
sr[i*k+j] = sorted[j];
}
}
jcs[n] = n*k;
}
And here is the called distEuclidean2:
function [ d ] = distEuclidean2( M, ii )
d = sqrt(sum((repmat(M(:, ii), 1, size(M, 2)) - M) .^ 2, 1));
emd
6 Kommentare
Walter Roberson
am 9 Jan. 2012
Suggest you edit the source in to your Question, as comments don't support formatting (or indentation even)
Akzeptierte Antwort
James Tursa
am 9 Jan. 2012
Bearbeitet: James Tursa
am 28 Jan. 2024
You might try using this to check the matrix:
href=""<http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok</a>>
Not sure if it works for 64-bit.
*** EDIT ***
Note that spok has been removed from the FEX. You can use my replacement function sarek instead:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) 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!