Question about the syntax of a MATLIB code line creating one array from another

55 visualizaciones (últimos 30 días)
Hello,
I am working on a MATLAB script that I inherited from a previous programmer and am trying to decipher the syntax of one line. This one line initializes an array called 'x' from another array called 'x0'. There is also a third array called 'indices' that contains the indices of "valid" values of x0. What "valid" means doesn't matter, but let's say for the sake of argument that x0 is a 1x15 array of doubles, and 'indices' is a 1x4 integer array with the values [2 4 7 9]. That's the background.
The assignment statement I have a question about is this, which I'll call Statement #1:
x = [x0(1); x0(indices); x0(end)];
I think the purpose of the above statement is to initialize 'x' to something like this:
x = [x0(1) x0(2) x0(4) x0(7) x0(9) x0(15)];
My question is about the syntax of Statement #1. With the semicolons on its right side, it seems like 'x' would become a 2-dimensional array, and, as I said above, I think the intent was for 'x' to become 1-dimensional array with numel(indices)+2 elements. I think the syntax of Statement #1 should therefore replace the semicolons with commas to give the desired result. Am I right?
  1 comentario
Stephen23
Stephen23 el 31 de Oct. de 2024 a las 6:27
Editada: Stephen23 el 31 de Oct. de 2024 a las 6:27
As you described it the code would throw an error:
x0 = 1:15; % 1x15
indices = [2,4,7,9]; % 1x4
[x0(1);x0(indices);x0(end)]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
"I think the syntax of Statement #1 should therefore replace the semicolons with commas to give the desired result."
Most likely the answer to your question is answered in the following code: what orientation does the rest of the code require?

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 31 de Oct. de 2024 a las 5:33
Movida: dpb el 31 de Oct. de 2024 a las 5:33
Well, what happens if you run the code as written? It's hard to tell for sure without the actual code in situ, but from the description
x0=1:15;
indices=[2 4 7 9];
x = [x0(1); x0(indices); x0(end)];
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
and, indeed, that as described is a syntax error.
Are you sure you didn't miss a transposition operator on x0? Or, is the Q because the code actually doesn't run as is; you don't say you got an error running it...
  4 comentarios
Michael McCully
Michael McCully el 1 de Nov. de 2024 a las 18:48
Done, thanks. FYI, I started on FORTRAN 66, myself, then went on to learn Pascal, then C from the K&R book, and so on. My first programs were on punch cards fed to a Honeywell mainframe that took up an entire room, and it had 64K of core memory. Now so much can be done on a handheld...
dpb
dpb el 2 de Nov. de 2024 a las 11:23
They wouldn't let mere undergraduates on the mainframes, Eng'g had an IBM 1620 standalone in the basement. Punch card in, punch card out; weren't allowed to access the IBM Selectric typewriter for output; one had to run the output card deck through the duplicator to be able to see the results on the top of the card or read the holes; no printer.
First mainframe was Philco 2000 w/ 27(!) 7-track tape drives, no drum or disk...

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 31 de Oct. de 2024 a las 5:30
To answer this, you have to know that the result of indexing a column vector with a single vector is a column vector, and the result of indexing a row vector with a single vector is a row vector:
A = [1 2 3]
A = 1×3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(2:3)
ans = 1×2
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A((2:3)')
ans = 1×2
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = [1; 2; 3]
B = 3×1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B(2:3)
ans = 2×1
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B((2:3)')
ans = 2×1
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So the shape of the indexing vector does not matter in this context.
The behaviour is different when it is a 2D array being indexed:
C = [1 2 3; 4 5 6]
C = 2×3
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C(2:3)
ans = 1×2
4 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C((2:3)')
ans = 2×1
4 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In this 2D case, the shape of the indexing vector does matter.
  1 comentario
dpb
dpb el 31 de Oct. de 2024 a las 12:11
That points out the thing I asked @Michael McCully about, @Walter Roberson of whether he's missing seeing a transpose operation somewhere such that his description of x0 as a row vector is incorrect in his actual code and that having only his description of the code isn't the same as having the code itself.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by