the dimension of matrix when using 'sparse' to generate sparse matrix

113 次查看(过去 30 天)
Why matlab has removed the high dimension operation of sparse?
I used to generate 3D sparse matrix using sparse. However, I find an error message when using this function says: Error using sparse, inputs must be 2-D.
Is this problem solvable?
  3 个评论
Torsten
Torsten 2025-12-6,16:45
I've never seen that MATLAB's "sparse" could be applied to N-dimensional arrays with N ~= 2. Can you give a link where this happened ? Do you mean the contribution from the file exchange
?
Walter Roberson
Walter Roberson 2025-12-6,20:57
The implementation of sparse() was stable for a quite long time, supporting only logical and double precision 2D matrices. As of R2025a, support was added for single precision matrices. But that's the only change in a long long time.

请先登录,再进行评论。

回答(3 个)

Umar
Umar 2025-12-7,1:18
编辑:Umar 2025-12-7,1:20
此 个回答 已被 Dyuman Joshi 标记

Hi @li,

I saw your question about MATLAB removing 3D sparse matrix support, and I wanted to help clarify what's happening. Both @Torsten and @Walter Roberson are absolutely correct in their responses to you. MATLAB's native sparse function has actually never supported 3D or N-dimensional arrays. It's always been limited to 2D matrices only, going all the way back to before R2006a.

I know this might be confusing because when you call something like sparse(3, 3, 3), MATLAB doesn't throw an error right away. Instead, it interprets this as the sparse(i, j, v) syntax, where i=3 is the row index, j=3 is the column index, and v=3 is the value. So you end up with a 2D matrix that has a single element at position (3,3) with value 3, not a 3D array at all. This might be why you thought it was working before.

The error message you're seeing now, " Error using sparse, inputs must be 2-D", is actually the function working correctly and enforcing its documented 2D-only limitation. Walter mentioned that the only recent change to sparse was in R2025a, when they added support for single precision matrices alongside the existing double and logical types. That's the first major enhancement in many years, and it had nothing to do with removing any dimensional capabilities.

As Torsten suggested, if you truly need 3D sparse functionality, you'll want to look at the N-dimensional sparse arrays contribution on the MATLAB File Exchange ( https://uk.mathworks.com/matlabcentral/fileexchange/29832-n-dimensional-sparse-arrays ). That's a third-party solution that provides the ndSparse class, which can handle the multidimensional sparse arrays that native MATLAB cannot.

I tested this myself to confirm, and here's some code that demonstrates the 2D limitation:

S1 = sparse(5, 5);
disp('2D sparse matrix created successfully');
disp(S1);
% This also works - 2D sparse with values
i = [1 2 3];
j = [1 2 3];
v = [10 20 30];
S2 = sparse(i, j, v, 5, 5);
disp('2D sparse matrix with values:');
disp(S2);
% This doesn't create 3D - it creates 2D with element at (3,3)
S3 = sparse(3, 3, 3);
disp('This is still 2D:');
disp(['Dimensions: ' num2str(size(S3))]);
disp(['Number of dimensions: ' num2str(ndims(S3))]);
% To truly get the error, try converting a 3D array
try
  A_3D = rand(3, 3, 3);
  S_3D = sparse(A_3D);
catch ME
  disp('Error when trying true 3D:');
  disp(ME.message);
end

Note: please see attached.

Hope this helps clar things up. The functionality you're looking for was never in base MATLAB, so nothing was actually removed. You'll need to use a File Exchange solution for 3D sparse arrays.

  4 个评论
Umar
Umar 2025-12-7,17:33

Hi @Catalytic,

I want to clarify the intent of my answer. My goal was to provide additional value beyond what was already stated by others:

I explicitly demonstrated the difference between sparse(3,3,3) and a true 3D array with a reproducible MATLAB script. This helps users see why MATLAB behaves the way it does.

I included a practical solution using cell arrays of sparse matrices for 3D data, which is not mentioned in the previous comments. I provided links to official documentation and historical MathWorks support, showing that 3D sparse matrices were never supported, which gives authoritative context.

The combination of explanation, demonstration, and practical workaround is meant to be a complete, step-by-step answer to help users understand the limitation and a working alternative.

I hope this clarifies why this answer adds unique value, rather than simply repeating earlier comments.Let me know what is your feedback to @Li comments, could you please contribute your thoughts, we will really appreciate it.

Umar
Umar 2025-12-7,20:19

Note: This answer has been revised in my updated response below, which incorporates technical clarifications from @Walter Roberson.

请先登录,再进行评论。


Umar
Umar 2025-12-7,17:47
编辑:Umar 2025-12-7,20:26

Hi @Li,

After going through recent comments, I wanted to provide a clear explanation of the situation regarding 3D sparse matrices in MATLAB, step by step.

You asked, “Why MATLAB does not support high-dimensional sparse matrices”

Feedback: MATLAB’s `sparse` function has never supported 3D or N-dimensional sparse arrays. Sparse matrices in MATLAB are always 2D (rows × columns). According to MathWorks Support (2009), “MATLAB only supports two-dimensional sparse matrices” [link]( < Mathworks > )

Also, the official `sparse` documentation also only describes 2D usage and makes no mention of N-dimensional variants ([link]( Mathworks )).

Therefore, there was never a “high-dimension operation” to remove; the function was never designed for that.

Edit comments: As @Walter Roberson demonstrated recently, this limitation is fundamental and not related to syntax conflicts:

sparse(ones(2,3,4))
Error using sparse
Inputs must be 2-D.

This definitively shows MATLAB rejects true 3D arrays regardless of the syntax used.

You asked, “Why you see the error “Inputs must be 2-D”

Feedback: When you pass a true 3D array to `sparse`, MATLAB rejects it, because the sparse format is strictly 2D. Even if you try to `reshape` a 1D or 2D sparse matrix into 3D, MATLAB internally flattens the extra dimensions, returning a 2D matrix rather than a true 3D sparse array.

You asked, “Practical solutions to mimic 3D sparse arrays”

Feedback: Since MATLAB cannot natively create 3D sparse matrices, there are two practical approaches:

1. Cell array of 2D sparse slices

You can create a 3D-like structure by storing each 2D slice as a separate sparse matrix inside a cell array. Example discussion here: [link]( Mathworks )

2. Use third-party N-D sparse implementations

The MATLAB File Exchange has a solution called `ndSparse` for true N-dimensional sparse arrays: [link]( < ndSparse > ) as already suggested by @Torsten in his comments section.

Demonstration Script

%% Step 1: Error for a true 3D array
try
  A_3D = rand(3,3,3);
  S_3D = sparse(A_3D);
catch ME
  disp('Error for true 3D array:');
  disp(ME.message);
end
%% Step 2: sparse(3,3,3) creates 2D matrix
S = sparse(3,3,3);
disp('Size of sparse(3,3,3):');
disp(size(S));
disp('Number of dimensions:');
disp(ndims(S));
%% Step 3: Mimic 3D sparse matrix with cell array
nSlices = 3;  
slices = cell(1,nSlices);
for k = 1:nSlices
  slices{k} = sparse(rand(3,3));  % 2D sparse slice
end
disp('Slice 3:');
disp(slices{3});
% Access an element
val = slices{2}(2,3);
disp(['Element (2,3) in slice 2: ', num2str(val)]);

Results: please see attached

Explanation: * True 3D array —> error: “Inputs must be 2‑D” * sparse(3,3,3) —> creates a 2D sparse matrix * Cell array —> successfully mimics a 3D sparse structure

This explanation is chronological and addresses both of your questions:

1. MATLAB never supported high-dimensional sparse matrices (so nothing was removed). 2. You can practically work around the 2D limitation using cell arrays or the “ndSparse” class.

Hope this clarifies everything and provides a concrete solution. If you need further assistance, please let us know.

  2 个评论
Walter Roberson
Walter Roberson 2025-12-7,18:49
Although sparse(rows, columns, pages) does not work, hypothetically the prime syntax sparse(A) could work for 3D A. But it does not work.
sparse(ones(2,3,4))
Error using sparse
Inputs must be 2-D.
This does not rely upon syntax conflicts, which themselves do not prove the point.
Umar
Umar 2025-12-7,20:15

Thank you @Walter Roberson for that crucial clarification. You're absolutely right— the sparse(ones(2,3,4)) example definitively demonstrates the 2D limitation without any syntax ambiguity. This is a much cleaner and more rigorous proof than my sparse(3,3,3) explanation.

I genuinely appreciate you taking the time to provide this correction, and I always respect and value your expertise in helping the MATLAB community understand these technical details correctly.

请先登录,再进行评论。


li
li 2025-12-8,6:29
I used to generate matrix like H=rand(8,8,20); and then i would like to use this 3D matrix to generate an block diagonal matrix using blkdiag(H(8,8,:)), or generate a sparse bolck diagonal matrix using spblkdiag provided by Rody Oldenhuis . Recently, I have reused my code to do calculations, however, this command cannot function any more. So I wounder if matlab has removed this function or changed it.
The version of my last used matlab software is about 2017b. However, I can't use my program in the new 2025a software.
Or maybe I have added the ndSparse funtion in my old software and I forgeted it.
I will try it and report the feedbacks.
Thanks for all of your help.
  13 个评论
li
li 15 minutes 前
Hi, @Umar,
I have applied your code into my code. However, I get this error.
Also, I have checked the tril version used in my code. It do contained in a ndSparese.m file.
Is it possible to mend the code?
Thank you very much!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by