diff --git a/ignite/metrics/confusion_matrix.py b/ignite/metrics/confusion_matrix.py
index 4325e3b8..3546796a 100644
--- a/ignite/metrics/confusion_matrix.py
+++ b/ignite/metrics/confusion_matrix.py
@@ -77,7 +77,7 @@ class ConfusionMatrix(Metric):
             y_ohe = to_onehot(y.reshape(-1), self.num_classes)
             y_ohe_t = y_ohe.transpose(0, 1).float()
         else:
-            y_ohe_t = y.transpose(1, -1).reshape(y.shape[1], -1).float()
+            y_ohe_t = y.transpose(0, 1).reshape(y.shape[1], -1).float()
 
         indices = torch.argmax(y_pred, dim=1)
         y_pred_ohe = to_onehot(indices.reshape(-1), self.num_classes)

Bug in ConfusionMatrix
When passing in the groundtruth `y` with the format of `shape (batch_size, num_categories, ...) and contains ground-truth class indices`, flattened one-hot encoding tensor `y_ohe_t` will result in wrong order with respect to that of prediction. https://github.com/pytorch/ignite/blob/fc85e25dc4f938d780b4c425acb2d40f6cac6f24/ignite/metrics/confusion_matrix.py#L79-L80

https://github.com/pytorch/ignite/blob/fc85e25dc4f938d780b4c425acb2d40f6cac6f24/ignite/metrics/confusion_matrix.py#L82-L83
For example:
```python
y_pred                                       # shape (B, C, H, W)
indices = torch.argmax(y_pred, dim=1)        # shape (B, H, W)
y_pred_ohe = to_onehot(indices.reshape(-1),  # shape (B*H*W)
                       self.num_classes)     # shape (B*H*W, C)

y                                      # shape (B, C, H, W), C: num of classes
y_ohe_t = (y.transpose(1, -1)          # shape (B, W, H, C)
            .reshape(y.shape[1], -1))  # reshape (B, W, H, C) into (C, B*W*H) and the value order is totally wrong
```
Expected behavior:
```python
y_ohe_t = y.transpose(0, 1).reshape(y.shape[1], -1)
# (B, C, H, W) --> (C, B, H, W) --> (C, B*H*W)
```
diff --git a/tests/ignite/metrics/test_confusion_matrix.py b/tests/ignite/metrics/test_confusion_matrix.py
index e8a4f2a8..e0d84b19 100644
--- a/tests/ignite/metrics/test_confusion_matrix.py
+++ b/tests/ignite/metrics/test_confusion_matrix.py
@@ -7,7 +7,7 @@ from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, r
 from ignite.exceptions import NotComputableError
 from ignite.metrics import ConfusionMatrix, IoU, mIoU
 from ignite.metrics.confusion_matrix import cmAccuracy, cmPrecision, cmRecall
-
+from ignite.utils import to_onehot
 
 import pytest
 
@@ -51,10 +51,9 @@ def test_multiclass_wrong_inputs():
 
 def test_multiclass_input_N():
     # Multiclass input data of shape (N, ) and (N, C)
-    def _test():
+    def _test_N():
         num_classes = 4
         cm = ConfusionMatrix(num_classes=num_classes)
-
         y_pred = torch.rand(10, num_classes)
         y = torch.randint(0, num_classes, size=(10,)).type(torch.LongTensor)
         cm.update((y_pred, y))
@@ -99,14 +98,66 @@ def test_multiclass_input_N():
         np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
         assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
 
+    def _test_NC():
+        num_classes = 4
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(10, num_classes)
+        y_labels = torch.randint(0, num_classes, size=(10,)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        num_classes = 10
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(4, num_classes)
+        y_labels = torch.randint(0, num_classes, size=(4, )).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        # 2-classes
+        num_classes = 2
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(4, num_classes)
+        y_labels = torch.randint(0, num_classes, size=(4,)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        # Batched Updates
+        num_classes = 5
+        cm = ConfusionMatrix(num_classes=num_classes)
+
+        y_pred = torch.rand(100, num_classes)
+        y_labels = torch.randint(0, num_classes, size=(100,)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+
+        batch_size = 16
+        n_iters = y.shape[0] // batch_size + 1
+
+        for i in range(n_iters):
+            idx = i * batch_size
+            cm.update((y_pred[idx: idx + batch_size], y[idx: idx + batch_size]))
+
+        np_y = y_labels.numpy().ravel()
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
     # check multiple random inputs as random exact occurencies are rare
     for _ in range(10):
-        _test()
+        _test_N()
+        _test_NC()
 
 
 def test_multiclass_input_NL():
     # Multiclass input data of shape (N, L) and (N, C, L)
-    def _test():
+    def _test_NL():
         num_classes = 4
         cm = ConfusionMatrix(num_classes=num_classes)
 
@@ -144,14 +195,56 @@ def test_multiclass_input_NL():
         np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
         assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
 
+    def _test_NCL():
+        num_classes = 4
+        cm = ConfusionMatrix(num_classes=num_classes)
+
+        y_pred = torch.rand(10, num_classes, 5)
+        y_labels = torch.randint(0, num_classes, size=(10, 5)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        num_classes = 10
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(4, num_classes, 5)
+        y_labels = torch.randint(0, num_classes, size=(4, 5)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        # Batched Updates
+        num_classes = 9
+        cm = ConfusionMatrix(num_classes=num_classes)
+
+        y_pred = torch.rand(100, num_classes, 7)
+        y_labels = torch.randint(0, num_classes, size=(100, 7)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+
+        batch_size = 16
+        n_iters = y.shape[0] // batch_size + 1
+
+        for i in range(n_iters):
+            idx = i * batch_size
+            cm.update((y_pred[idx: idx + batch_size], y[idx: idx + batch_size]))
+
+        np_y = y_labels.numpy().ravel()
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
     # check multiple random inputs as random exact occurencies are rare
     for _ in range(10):
-        _test()
+        _test_NL()
+        _test_NCL()
 
 
 def test_multiclass_input_NHW():
     # Multiclass input data of shape (N, H, W, ...) and (N, C, H, W, ...)
-    def _test():
+    def _test_NHW():
         num_classes = 5
         cm = ConfusionMatrix(num_classes=num_classes)
 
@@ -188,9 +281,50 @@ def test_multiclass_input_NHW():
         np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
         assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
 
+    def _test_NCHW():
+        num_classes = 5
+        cm = ConfusionMatrix(num_classes=num_classes)
+
+        y_pred = torch.rand(4, num_classes, 12, 10)
+        y_labels = torch.randint(0, num_classes, size=(4, 12, 10)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        num_classes = 5
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(4, num_classes, 10, 12, 8)
+        y_labels = torch.randint(0, num_classes, size=(4, 10, 12, 8)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+        cm.update((y_pred, y))
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        np_y = y_labels.numpy().ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
+        # Batched Updates
+        num_classes = 3
+        cm = ConfusionMatrix(num_classes=num_classes)
+        y_pred = torch.rand(100, num_classes, 8, 8)
+        y_labels = torch.randint(0, num_classes, size=(100, 8, 8)).type(torch.LongTensor)
+        y = to_onehot(y_labels, num_classes=num_classes)
+
+        batch_size = 16
+        n_iters = y.shape[0] // batch_size + 1
+
+        for i in range(n_iters):
+            idx = i * batch_size
+            cm.update((y_pred[idx: idx + batch_size], y[idx: idx + batch_size]))
+
+        np_y = y_labels.numpy().ravel()
+        np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
+        assert np.all(confusion_matrix(np_y, np_y_pred, labels=list(range(num_classes))) == cm.compute().numpy())
+
     # check multiple random inputs as random exact occurencies are rare
     for _ in range(10):
-        _test()
+        _test_NHW()
+        _test_NCHW()
 
 
 def get_y_true_y_pred():
@@ -445,9 +579,9 @@ def test_cm_recall():
 
 
 def test_cm_with_average():
-    num_classes = 4
-    y_pred = torch.rand(10, num_classes)
-    y = torch.randint(0, num_classes, size=(10,)).type(torch.LongTensor)
+    num_classes = 5
+    y_pred = torch.rand(20, num_classes)
+    y = torch.randint(0, num_classes, size=(20,)).type(torch.LongTensor)
     np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
     np_y = y.numpy().ravel()
 
@@ -459,12 +593,12 @@ def test_cm_with_average():
 
     cm = ConfusionMatrix(num_classes=num_classes, average='recall')
     cm.update((y_pred, y))
-    true_re = recall_score(np_y, np_y_pred, average=None)
+    true_re = recall_score(np_y, np_y_pred, average=None, labels=list(range(num_classes)))
     res = cm.compute().numpy().diagonal()
     np.testing.assert_almost_equal(true_re, res)
 
     cm = ConfusionMatrix(num_classes=num_classes, average='precision')
     cm.update((y_pred, y))
-    true_pr = precision_score(np_y, np_y_pred, average=None)
+    true_pr = precision_score(np_y, np_y_pred, average=None, labels=list(range(num_classes)))
     res = cm.compute().numpy().diagonal()
     np.testing.assert_almost_equal(true_pr, res)

pip install -e .
parse_log_pytest
pytest
numpy mock pytest codecov pytest-cov matplotlib pandas gym tqdm scikit-learn tensorboardX visdom polyaxon-client mlflow
apt-get update apt-get install -y gcc
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alembic==1.7.7
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
certifi==2021.5.30
charset-normalizer==2.0.12
click==7.1.2
cloudpickle==2.2.1
codecov==2.1.13
coverage==6.2
cycler==0.11.0
databricks-cli==0.17.8
dataclasses==0.8
decorator==4.4.2
docker==5.0.3
entrypoints==0.4
Flask==1.1.4
gitdb==4.0.9
GitPython==3.1.18
greenlet==2.0.2
gunicorn==21.2.0
gym==0.26.2
gym-notices==0.0.8
hestia==0.6.0
idna==3.10
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1631916693255/work
importlib-resources==5.4.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
itsdangerous==1.1.0
Jinja2==2.10.3
joblib==1.1.1
jsonpatch==1.32
jsonpointer==2.3
kiwisolver==1.3.1
Mako==1.1.6
MarkupSafe==2.0.1
marshmallow==3.0.0rc5
matplotlib==3.3.4
mlflow==1.23.1
mock==5.2.0
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
networkx==2.5.1
numpy==1.19.5
oauthlib==3.2.2
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pandas==1.1.5
Pillow==8.4.0
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
polyaxon-client==0.6.1
polyaxon-schemas==0.6.1
polystores==0.2.5
prometheus-client==0.17.1
prometheus_flask_exporter==0.23.2
protobuf==4.21.0
psutil==7.0.0
py @ file:///opt/conda/conda-bld/py_1644396412707/work
PyJWT==2.4.0
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
pytest-cov==4.0.0
python-dateutil==2.9.0.post0
-e git+https://github.com/pytorch/ignite.git@fc85e25dc4f938d780b4c425acb2d40f6cac6f24#egg=pytorch_ignite
pytz==2025.2
PyYAML==6.0.1
querystring-parser==1.2.4
requests==2.27.1
requests-toolbelt==1.0.0
rhea==0.5.5
scikit-learn==0.24.2
scipy==1.5.4
six==1.17.0
smmap==5.0.0
SQLAlchemy==1.4.54
sqlparse==0.4.4
tabulate==0.8.10
tensorboardX==2.6.2.2
threadpoolctl==3.1.0
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
tomli==1.2.3
torch==1.10.2
tornado==6.1
tqdm==4.64.1
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
urllib3==1.26.20
visdom==0.2.4
websocket-client==1.3.1
Werkzeug==1.0.1
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work

name: ignite
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib-metadata=4.8.1=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alembic==1.7.7
      - charset-normalizer==2.0.12
      - click==7.1.2
      - cloudpickle==2.2.1
      - codecov==2.1.13
      - coverage==6.2
      - cycler==0.11.0
      - databricks-cli==0.17.8
      - dataclasses==0.8
      - decorator==4.4.2
      - docker==5.0.3
      - entrypoints==0.4
      - flask==1.1.4
      - gitdb==4.0.9
      - gitpython==3.1.18
      - greenlet==2.0.2
      - gunicorn==21.2.0
      - gym==0.26.2
      - gym-notices==0.0.8
      - hestia==0.6.0
      - idna==3.10
      - importlib-resources==5.4.0
      - itsdangerous==1.1.0
      - jinja2==2.10.3
      - joblib==1.1.1
      - jsonpatch==1.32
      - jsonpointer==2.3
      - kiwisolver==1.3.1
      - mako==1.1.6
      - markupsafe==2.0.1
      - marshmallow==3.0.0rc5
      - matplotlib==3.3.4
      - mlflow==1.23.1
      - mock==5.2.0
      - networkx==2.5.1
      - numpy==1.19.5
      - oauthlib==3.2.2
      - pandas==1.1.5
      - pillow==8.4.0
      - polyaxon-client==0.6.1
      - polyaxon-schemas==0.6.1
      - polystores==0.2.5
      - prometheus-client==0.17.1
      - prometheus-flask-exporter==0.23.2
      - protobuf==4.21.0
      - psutil==7.0.0
      - pyjwt==2.4.0
      - pytest-cov==4.0.0
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - pyyaml==6.0.1
      - querystring-parser==1.2.4
      - requests==2.27.1
      - requests-toolbelt==1.0.0
      - rhea==0.5.5
      - scikit-learn==0.24.2
      - scipy==1.5.4
      - six==1.17.0
      - smmap==5.0.0
      - sqlalchemy==1.4.54
      - sqlparse==0.4.4
      - tabulate==0.8.10
      - tensorboardx==2.6.2.2
      - threadpoolctl==3.1.0
      - tomli==1.2.3
      - torch==1.10.2
      - tornado==6.1
      - tqdm==4.64.1
      - urllib3==1.26.20
      - visdom==0.2.4
      - websocket-client==1.3.1
      - werkzeug==1.0.1
prefix: /opt/conda/envs/ignite

tests/ignite/metrics/test_confusion_matrix.py::test_multiclass_input_N tests/ignite/metrics/test_confusion_matrix.py::test_multiclass_input_NL tests/ignite/metrics/test_confusion_matrix.py::test_multiclass_input_NHW
tests/ignite/metrics/test_confusion_matrix.py::test_no_update tests/ignite/metrics/test_confusion_matrix.py::test_multiclass_wrong_inputs tests/ignite/metrics/test_confusion_matrix.py::test_multiclass_images tests/ignite/metrics/test_confusion_matrix.py::test_iou_wrong_input tests/ignite/metrics/test_confusion_matrix.py::test_iou tests/ignite/metrics/test_confusion_matrix.py::test_miou tests/ignite/metrics/test_confusion_matrix.py::test_cm_accuracy tests/ignite/metrics/test_confusion_matrix.py::test_cm_precision tests/ignite/metrics/test_confusion_matrix.py::test_cm_recall tests/ignite/metrics/test_confusion_matrix.py::test_cm_with_average
ignite/metrics/confusion_matrix.py
ignite/metrics/confusion_matrix.py