how can i go through a matrix line by line?
I have this matrix and would like to have the value of a spinner depending on the value.
x = [1:1:5; 6:1:10]

 採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 20 日

0 投票

x = [1:1:5; 6:1:10; 11:15; 16:20; 21:25; 26:30; 31:35; [36, 0, -1, nan, nan]];
nrow = size(x,1);
for K = 1 : 5
spin = randi(nrow);
randrow = x(spin,:)
end
randrow = 1×5
11 12 13 14 15
randrow = 1×5
26 27 28 29 30
randrow = 1×5
26 27 28 29 30
randrow = 1×5
11 12 13 14 15
randrow = 1×5
31 32 33 34 35

3 件のコメント

TheDice
TheDice 2021 年 4 月 20 日
ok maybe i didn't write that correctly. when i enter 6 in my spinner i want the value from the second line in the first column.
if I query the so x(6) I get yes the value from the second row third column
Walter Roberson
Walter Roberson 2021 年 4 月 20 日
It sounds like you want linear indexing, but row by row
x = [1:1:5; 6:1:10; 11:15; 16:20; 21:25; 26:30; 31:35; [36, 0, -1, nan, nan]];
nent = numel(x);
xt = x.';
for K = 1 : 5
spin = randi(nent);
randval = xt(spin);
[spin, randval]
end
ans = 1×2
1 1
ans = 1×2
37 0
ans = 1×2
19 19
ans = 1×2
35 35
ans = 1×2
30 30
The entries in the array happen to all be equal to their index when operating with that scheme, except for the last few, and you can see in my example that indeed it happened to pull out the stored value (0) at index 37
TheDice
TheDice 2021 年 4 月 20 日
Thank you. Is it possible to fill the matrix from top to bottom not from left to right?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2021 年 4 月 20 日

コメント済み:

2021 年 4 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by