How to use cat properly

Hi
I'm confused about the properly way of using cat and the help isn't being so helpful. Let's say that I have a variable var1
Name size
var1 326x377x31x9.
I want to rearrange "passing down" the last dimension. I know that this words make no sense in a matrix context, but please bear with me.
So the new size would be (31x9=279)
Name size
var2 326x377x279
I'm currently using
var2 = cat(XX,var1(:,:,:));
If I use XX as 1,2,3 or 4, I get the same result, this is, the size I want. But, what does it mean in this context if I use XX as 1, 2, 3 or 4?
Thanks.

 Accepted Answer

Matt J
Matt J on 29 Mar 2022
Edited: Matt J on 29 Mar 2022
The cat command is not causing the change that you are seeing in the array shape. What's causing it is the fact that you are indexing with 3 subscripts (:,:,:) instead of 4. Since the cat command only receives 2 arguments it simply returns the 2nd argument unchanged. You could have equivalently generated var2 without cat as follows:
var1=rand(326,377,31,9);
var2=var1(:,:,:);
whos var1 var2
Name Size Bytes Class Attributes var1 326x377x31x9 274317264 double var2 326x377x279 274317264 double
To do something meaingful with cat(), you must give it 3 or more input arguments, e.g.,
var1=eye(3); var2=rand(3);
cat(1, var1,var2) %cat along dimension 1
ans = 6×3
1.0000 0 0 0 1.0000 0 0 0 1.0000 0.0992 0.6564 0.8313 0.5780 0.2544 0.3718 0.1110 0.8576 0.2186
cat(2,var1,var2) %cat along dimension 2
ans = 3×6
1.0000 0 0 0.0992 0.6564 0.8313 0 1.0000 0 0.5780 0.2544 0.3718 0 0 1.0000 0.1110 0.8576 0.2186
cat(3,var1,var2) %cat along dimension 3
ans =
ans(:,:,1) = 1 0 0 0 1 0 0 0 1 ans(:,:,2) = 0.0992 0.6564 0.8313 0.5780 0.2544 0.3718 0.1110 0.8576 0.2186

5 Comments

Instead of using cat take a look at the reshape function.
x = 1:12
x = 1×12
1 2 3 4 5 6 7 8 9 10 11 12
y = reshape(x, 3, 4)
y = 3×4
1 4 7 10 2 5 8 11 3 6 9 12
z = reshape(x, 4, 3).'
z = 3×4
1 2 3 4 5 6 7 8 9 10 11 12
user20912
user20912 on 29 Mar 2022
Edited: user20912 on 29 Mar 2022
EDIT: I have answer too fast, sorry.
Thanks for your help! I see in what I was wrong. I goint to try with the reshape function.
The cat command has no role in what you are seeing. When you pass the cat command only 2 arguments it simply returns the 2nd argument unchanged. You could have equivalently generated var2 without cat as follows:
var1=rand(326,377,31,9);
var2=var1(:,:,:);
whos var1 var2
Name Size Bytes Class Attributes var1 326x377x31x9 274317264 double var2 326x377x279 274317264 double
or, more generally, you could have used reshape() as Steven mentioned.
Dear Matt, when you say
or, more generally, you could have used reshape() as Steven mentioned.
do you mean that
var2=var1(:,:,:);
is the same as the following?
[x,y,~] = size(var1)
var2 = reshape(var1,x,y,[])
Because my results are not changing.
Thanks in advance
Yes, it's the same.

Sign in to comment.

More Answers (0)

Products

Release

R2017a

Asked:

on 29 Mar 2022

Commented:

on 29 Mar 2022

Community Treasure Hunt

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

Start Hunting!