Extracting a Column from a 4D Double Array in MATLAB using Squeeze

In summary, the conversation discusses accessing a column of values from the 4th dimension of a 4D Double array in MATLAB and saving them to a new matrix. The correct use of the colon operator is explained and the solution is to use the squeeze function.
  • #1
afallingbomb
16
0
Hello,

I'd like to access a column of values from the 4th dimension of a 4D Double array in MATLAB and then save them to a new matrix.

For example:

A = rand(3,3,3,3);
A(1,1,1,:)

gives me:

ans(:,:,1,1) =
0.7077

ans(:,:,1,2) =
0.0669

ans(:,:,1,3) =
0.7794

I want to create a new matrix, B, with those values but specifying B = A(1,1,1,:) results in the same output above. I want a vector in this form:

0.7077
0.0669
0.7794

Thank you!
 
Physics news on Phys.org
  • #2
Your use of the colon operator is incorrect. See the Matlab docs on this subject for an explanation of how to subscript an array in the way you want.
 
  • #3
Just put squeeze in front.

squeeze(A(1,1,1,:))
 
  • #4
Thanks, Matonski. Squeeze does the trick!
 

Q1: What is a MATLAB multidimensional array?

A MATLAB multidimensional array is a data structure that can hold multiple elements of the same data type. It is similar to a regular array, but it can have more than one dimension, allowing for the storage of data in a grid or matrix format.

Q2: How do I create a multidimensional array in MATLAB?

To create a multidimensional array in MATLAB, you can use the "zeros" function, which takes in the desired dimensions of your array as input. For example, to create a 3x3x3 array, you would use the command "A = zeros(3,3,3)".

Q3: How do I access and modify elements in a multidimensional array?

You can access elements in a multidimensional array by using their indices. For example, to access the element in the first row and second column of a 2D array A, you would use the command "A(1,2)". To modify an element, you can use the same syntax but assign a new value to it, such as "A(1,2) = 5" to change the value to 5.

Q4: Can I perform mathematical operations on multidimensional arrays in MATLAB?

Yes, MATLAB allows for various mathematical operations to be performed on multidimensional arrays, such as addition, subtraction, multiplication, and division. These operations can be performed on the entire array or on specific elements using the same syntax as accessing and modifying elements.

Q5: Are there any functions specifically for working with multidimensional arrays in MATLAB?

Yes, there are several functions in MATLAB that are designed for working with multidimensional arrays. Some examples include "sum" for calculating the sum of all elements, "reshape" for changing the dimensions of an array, and "transpose" for transposing the array. You can also use the "size" function to determine the dimensions of an array.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
21K
  • Introductory Physics Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top