Saving multiple single vectors in one Matlab file

1 visualización (últimos 30 días)
Gobert
Gobert el 4 de Oct. de 2022
Comentada: Cris LaPierre el 5 de Oct. de 2022
Hi,
I need your help:
Here's a simple example:
A = magic(3);
B = padarray(A,[1 1]);
[row, col] = size(B);
for i = 2:row-1
for j = 2:col-1
C = [B(i-1,j-1),B(i-1,j),B(i-1,j+1),B(i,j-1),B(i,j),B(i,j+1),B(i+1,j-1),B(i+1,j),B(i+1,j+1)]
save C.mat
end
end
When you run the above code in Matlab, you will get the following output in the command window
C = 0 0 0 0 8 1 0 3 5
C = 0 0 0 8 1 6 3 5 7
C = 0 0 0 1 6 0 5 7 0
C = 0 8 1 0 3 5 0 4 9
C = 8 1 6 3 5 7 4 9 2
C = 1 6 0 5 7 0 9 2 0
C = 0 3 5 0 4 9 0 0 0
C = 3 5 7 4 9 2 0 0 0
C = 5 7 0 9 2 0 0 0 0
I would like to save the following output in C.mat file using "save" function
0 0 0 0 8 1 0 3 5
0 0 0 8 1 6 3 5 7
0 0 0 1 6 0 5 7 0
0 8 1 0 3 5 0 4 9
8 1 6 3 5 7 4 9 2
1 6 0 5 7 0 9 2 0
0 3 5 0 4 9 0 0 0
3 5 7 4 9 2 0 0 0
5 7 0 9 2 0 0 0 0
However, when I use "save" C.mat, I can only output and save one line, shown below.
5 7 0 9 2 0 0 0 0
Can you help to correct the above code to be able to save the above 9-by-9 matrix in C.mat file?

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 4 de Oct. de 2022
You overwrite your variable C in each loop, and you overwrite C.mat as well, so the final contents are just what was created in the last loop. I would do something like the following to append the result of each loop to C rather than replace.
A = magic(3);
B = padarray(A,[1 1]);
[row, col] = size(B);
C = [];
for i = 2:row-1
for j = 2:col-1
C = [C;B(i-1,j-1),B(i-1,j),B(i-1,j+1),B(i,j-1),B(i,j),B(i,j+1),B(i+1,j-1),B(i+1,j),B(i+1,j+1)];
end
end
C
C = 9×9
0 0 0 0 8 1 0 3 5 0 0 0 8 1 6 3 5 7 0 0 0 1 6 0 5 7 0 0 8 1 0 3 5 0 4 9 8 1 6 3 5 7 4 9 2 1 6 0 5 7 0 9 2 0 0 3 5 0 4 9 0 0 0 3 5 7 4 9 2 0 0 0 5 7 0 9 2 0 0 0 0
save C.mat
% Check that C.mat contains all 9 values
clear
load C.mat
C
C = 9×9
0 0 0 0 8 1 0 3 5 0 0 0 8 1 6 3 5 7 0 0 0 1 6 0 5 7 0 0 8 1 0 3 5 0 4 9 8 1 6 3 5 7 4 9 2 1 6 0 5 7 0 9 2 0 0 3 5 0 4 9 0 0 0 3 5 7 4 9 2 0 0 0 5 7 0 9 2 0 0 0 0
  3 comentarios
Gobert
Gobert el 5 de Oct. de 2022
Editada: Gobert el 5 de Oct. de 2022
There is a little concern about the above code. I notice that it saves everything. For example, for example, when I used A = magic(5), and loaded the C.mat file, I got the below output. Of course, I can use X.C to only access the content of C. However, I do not want my .mat file to contain other details other than those in C. Therefore, can you please suggest modifications to your code to only single out and SAVE the content of C?
X = load('C.mat')
X =
struct with fields:
A: [5×5 double]
B: [7×7 double]
C: [25×9 double]
col: 7
i: 6
j: 6
row: 7
Cris LaPierre
Cris LaPierre el 5 de Oct. de 2022
There are multiple syntaxes you can use when calling save. Check out the documentation page to learn about each. The one you want is

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by