如何找到哪些列被删除?

时间:2015-05-19 12:42:23

标签: matlab

假设我有一个矩阵, 然后我通过

找到哪些列的方差为零
contextMenuItem.hbmpItem = IconToBitmap(pathToIcon);

// the function from your posted link

HBITMAP IconToBitmap(std::string sIcon) 
{ 
    RECT rect;
    rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);
    rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);

    rect.left = rect.top  = 0;

    HICON hIcon = (HICON)LoadImageA(NULL, sIcon.c_str(), IMAGE_ICON,    rect.right, rect.bottom, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
    if (!hIcon)
        return NULL;

    HWND desktop = ::GetDesktopWindow();
    if (desktop == NULL)
    {
        DestroyIcon(hIcon);
        return NULL;
    }

    HDC screen_dev = ::GetDC(desktop);
    if (screen_dev == NULL)
    {
        DestroyIcon(hIcon);
       return NULL;
    }

    // Create a compatible DC
    HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
    if (dst_hdc == NULL)
    {
        DestroyIcon(hIcon);
        ::ReleaseDC(desktop, screen_dev);
        return NULL;
    }

    // Create a new bitmap of icon size
    HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
    if (bmp == NULL)
    {
        DestroyIcon(hIcon);
        ::DeleteDC(dst_hdc);
        ::ReleaseDC(desktop, screen_dev);
        return NULL;
    }

    // Select it into the compatible DC
    HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
    if (old_dst_bmp == NULL)
    {
        DestroyIcon(hIcon);
        return NULL;
    }

    // Fill the background of the compatible DC with the given colour
    ::SetBkColor(dst_hdc, RGB(255, 255, 255));
    ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);

    // Draw the icon into the compatible DC
    ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);

    // Restore settings
    ::SelectObject(dst_hdc, old_dst_bmp);
    ::DeleteDC(dst_hdc);
    ::ReleaseDC(desktop, screen_dev);
    DestroyIcon(hIcon);
    return bmp; 

然后我删除零方差的那些。

我想知道方差为零的ID

1 个答案:

答案 0 :(得分:1)

如果你在计算的立场上谈论它是非常危险的,因为有些值它们不是很零,但它们也可能是因为浮点精度。我建议你做的是与一个小容差进行比较...说... 1e-20

确定此公差后,您只需将方差计算为正常,但检查每列是否小于此公差。 var默认情况下会计算2D矩阵中每列的方差,所以请执行以下操作:

tol = 1e-20;
check = var(X) < tol;

check将包含logical数组,其中0表示一列不是&#34;零&#34;并且1表示它是。如果您需要物理列号,可以拨打find

numbers = find(check);

接下来,如果您要删除列,只需使用您在上方创建的X数组和logical数组编入null索引:

X(:, check) = [];