for文とif 文

4 views (last 30 days)
sz on 25 Oct 2021
Edited: Walter Roberson on 25 Oct 2021

そこからif 文やfor 文を使って下記の計算をしたいです
1列目の数字がもし１だったら１×２　０だったら０×０
2列目の数字がもし１だったら１×２^2　０だったら０×０
3列目の数字がもし１だったら１×２^3　　０だったら０×０
そしてその数字を足していっていただきたいです。

これが１００行分あるのでその計算をしていきたいので可能でしたらスクリプトを教えていただきたいです。
１００×３のデータは下記のスクリプトのCになります。
よろしくお願いいたします。
rng(1,'philox')
X = randi([0 1], 3, 3, 100);
a = tril(ones(3), -1) == 1;
a = repmat(a, 1, 1, 100);
X(a) = 0;
X = X + permute(X,[2 1 3]);
a = eye(3) == 1;
a = repmat(a, 1, 1, 100);
X(a) = 1;
A = reshape(permute(X, [2 1 3]), 1, [], 100);
B = squeeze(A)';
C=B(:,[4,7,8]);
Walter Roberson on 25 Oct 2021
Approximate translation:
Currently, there is data with only 1 and 0 numbers of 100x3.
From there, I want to do the following calculations using if and for statements
If the number in the first column is 1, it is 1x2. If it is 0, it is 0x0.
If the number in the second column is 1, 1x2 ^ 2. If it is 0, 0x0
If the number in the third column is 1, then 1x2 ^ 3; if it is 0, then 0x0
And I would like you to add those numbers.
Example)
In the case of 1, 1, 0, 1 × 2 + 1 × 2 ^ 2 + 0 = 6
Since this is for 100 lines, I would like to calculate it, so please tell me the script if possible.The 100x3 data will be C in the script below.
Thank you.

Walter Roberson on 25 Oct 2021
Edited: Walter Roberson on 25 Oct 2021
rng(1,'philox')
X = randi([0 1], 3, 3, 100);
a = tril(ones(3), -1) == 1;
a = repmat(a, 1, 1, 100);
X(a) = 0;
X = X + permute(X,[2 1 3]);
a = eye(3) == 1;
a = repmat(a, 1, 1, 100);
X(a) = 1;
A = reshape(permute(X, [2 1 3]), 1, [], 100);
B = squeeze(A)';
C=B(:,[4,7,8]);
C(1:5,:)
ans = 5×3
0 1 1 0 0 0 0 0 0 1 1 1 0 0 0
D = C * 2.^(1:3).'
D = 100×1
12 0 0 14 0 2 0 4 12 8