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

55 ビュー (過去 30 日間)
Michael McCully
Michael McCully 2024 年 10 月 31 日 4:58
コメント済み: dpb 2024 年 11 月 2 日 11:23
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 件のコメント
Stephen23
Stephen23 2024 年 10 月 31 日 6:27
編集済み: Stephen23 2024 年 10 月 31 日 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?

サインインしてコメントする。

採用された回答

dpb
dpb 2024 年 10 月 31 日 5:33
移動済み: dpb 2024 年 10 月 31 日 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 件のコメント
Michael McCully
Michael McCully 2024 年 11 月 1 日 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 2024 年 11 月 2 日 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...

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 10 月 31 日 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 件のコメント
dpb
dpb 2024 年 10 月 31 日 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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by