for文とif 文
조회 수: 3 (최근 30일)
이전 댓글 표시
現在100×3 の数字が1と0のみのデータがあります。
そこからif 文やfor 文を使って下記の計算をしたいです
1列目の数字がもし1だったら1×2 0だったら0×0
2列目の数字がもし1だったら1×2^2 0だったら0×0
3列目の数字がもし1だったら1×2^3 0だったら0×0
そしてその数字を足していっていただきたいです。
例)1、1、0の場合、1×2+1×2^2+0=6
これが100行分あるのでその計算をしていきたいので可能でしたらスクリプトを教えていただきたいです。
100×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]);
댓글 수: 1
Walter Roberson
2021년 10월 25일
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
2021년 10월 25일
편집: Walter Roberson
2021년 10월 25일
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,:)
D = C * 2.^(1:3).'
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Adding custom doc에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!