# How to calculate occurrence of 3 variables

1 vue (au cours des 30 derniers jours)
Gaetan Dufour le 13 Juil 2018
Réponse apportée : Nihal le 21 Juin 2024
Hello, I have a time series with 3 columns : Wave height (H), Peak period (T) and Direction (D). Usually I create bivariate frequency tables of H/T and H/D, but now I would like to compute the occurrence of H/T/D triplets for several classes in order to get a table like: H T D Occurrence 0.5 7.2 180 10% 1.2 8.2 220 7% and so on...
Thank you!
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

Nihal le 21 Juin 2024
The Scytale cipher is an ancient transposition cipher used by the Spartans. It involves wrapping a strip of parchment around a rod (scytale) of a certain diameter and writing the message along the rod. When unwrapped, the message becomes scrambled. To decrypt it, you need a rod of the same diameter.
To apply the Scytale method for image encryption and decryption in MATLAB, you can treat the image as a sequence of bytes and perform the transposition accordingly. Below is an example of how to implement this in MATLAB:
Encryption
1. Read the Image: Convert the image into a 1D array.
2. Determine the Key: The key is the number of columns (or the diameter of the rod).
3. Encrypt the Image: Rearrange the pixels according to the Scytale cipher.
Decryption
1. Read the Encrypted Image: Convert the encrypted image into a 1D array.
2. Determine the Key: Use the same key as for encryption.
3. Decrypt the Image: Rearrange the pixels back to their original order.
Here is the complete MATLAB code to perform image encryption and decryption using the Scytale method:
function scytale_image_encryption_decryption()
[rows, cols, channels] = size(img);
total_pixels = rows * cols * channels;
% Convert the image to a 1D array
img_1d = img(:);
% Define the key (number of columns for the Scytale cipher)
key = 100; % Choose an appropriate key based on your image size
% Encrypt the image
encrypted_img_1d = scytale_encrypt(img_1d, key);
% Convert the encrypted 1D array back to an image
encrypted_img = reshape(encrypted_img_1d, [rows, cols, channels]);
imwrite(encrypted_img, 'encrypted_image.png'); % Save the encrypted image
% Decrypt the image
decrypted_img_1d = scytale_decrypt(encrypted_img_1d, key);
% Convert the decrypted 1D array back to an image
decrypted_img = reshape(decrypted_img_1d, [rows, cols, channels]);
imwrite(decrypted_img, 'decrypted_image.png'); % Save the decrypted image
% Display the original, encrypted, and decrypted images
figure;
subplot(1, 3, 1);
imshow(img);
title('Original Image');
subplot(1, 3, 2);
imshow(encrypted_img);
title('Encrypted Image');
subplot(1, 3, 3);
imshow(decrypted_img);
title('Decrypted Image');
end
function encrypted = scytale_encrypt(data, key)
% Calculate the number of rows needed
num_rows = ceil(length(data) / key);
% Pad the data if necessary
% Reshape the data into a matrix
% Transpose the matrix to perform the Scytale encryption
encrypted_matrix = matrix';
% Convert the encrypted matrix back to a 1D array
encrypted = encrypted_matrix(:);
end
function decrypted = scytale_decrypt(data, key)
% Calculate the number of rows needed
num_rows = ceil(length(data) / key);
% Reshape the encrypted data into a matrix
encrypted_matrix = reshape(data, [num_rows, key]);
% Transpose the matrix to perform the Scytale decryption
decrypted_matrix = encrypted_matrix';
% Convert the decrypted matrix back to a 1D array
decrypted = decrypted_matrix(:);
decrypted = decrypted(1:find(decrypted, 1, 'last'));
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Encryption / Cryptography dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by