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

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 Comment

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?

Sign in to comment.

 Accepted Answer

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 Comments

Sorry, I when I asked the question I wasn't at a computer I where could run the code, I was just looking at a listing and, as it were, "thinking out loud" (on a keyboard). However, now that I have tried it, the code does throw a syntax error as-is. It is also corrected by changing the semicolons to commas.
For the record, the rest of the code requires x0 to stay in its original orientation of 1xN, and for some other 1xM arrays to be created during computations. Thanks to all for the comments and answers to a simple question.
I guess I should have waited and answered my own question, but on the other hand, if someone who knew MATLAB were with me at the time, I would have asked that someone. Again, thanks to all of you for the comments and questions.
No problem, my first course introducing us to programming (pre-FORTRAN IV using McCracken) was all on paper with the then ubiquitous coding forms and such manual hand-compiling/executing instructions, so is a time honored practice. :)
If this or one of the others you judge more suitable closes out the Q?, it would be good to Accept an Answer so others can now the issue is resolved if for no other reason...
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...
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...

Sign in to comment.

More Answers (1)

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 Comment

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.

Sign in to comment.

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 31 Oct 2024

Commented:

dpb
on 2 Nov 2024

Community Treasure Hunt

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

Start Hunting!