I am working in Matlab.
I created a matrix and then a cell array based on that matrix:
A = randi([1 5], 5, 5);
B = num2cell(A);
I want to replace the numbers in B with different strings based on A's columns.
For example, where column
A(:,1) == 1
replace with 'Cat' and
A(:,2) == 1 replace with 'Dog'
If
A = [ 1 2 1 3 5; 2 1 5 1 4; 1 1 2 5 1; 5 5 3 4 1; 1 1 2 5 2]
Then I want
B = [ Cat 2 1 3 5; 2 Dog 5 1 4; Cat Dog 2 5 1; 5 5 3 4 1; Cat Dog 2 5 2]
I have tried
B(A(:, 1) == 1) = {'Cat'} and
B(A(:,2) == 1) = {'Dog'} but everything keeps getting placed in B's first column. I can't figure out the indexing.
Appreciate the help!
B(A(:,1) == 1, 1) = {'Cat'}should work. Or evenB(A(:,1) == 1, 1) = 'Cat'B(A(:,1) == 1, 1) = {'Cat'}andB(A(:,2) == 1, 2) = {'Dog'}did the trick!