## How to convert each row of the matrix into a Character array, without using a for command?

Jian Shi

### Jian Shi (view profile)

님이 질문을 제출함. 3 Apr 2019
최근 활동 dpb

### dpb (view profile)

님이 편집함. 6 Apr 2019
dpb

### dpb (view profile)

님이 답변을 채택함.
Matlab code:
A=rand(1e3,2);%just an example
for i=1:size(A,1)
B{i}=num2str(A(i,:));
end
Question: The cost time of the code is related to the size of A. How can I get the B without the For command and spend as less time as possible?

로그인 to comment.

## 답변 수: 1

dpb

### dpb (view profile)

님의 답변 3 Apr 2019
채택된 답변

Preallocate B may help just tad...
Alternatives to loop include
B=cellstr(num2str(A));
but what is end purpose for doing the conversion? Perhaps could avoid entirely??? The most time-saving optimization is the one that avoids doing the unneeded... :)

Jian Shi

### Jian Shi (view profile)

3 Apr 2019
The end purpose is to set up an one-to one mapping between (X,Y) and Z, where X,Y,Z is an array. To realist this, I only know the function containers.Map and the variable of the function must be char, double or some else.
dpb

### dpb (view profile)

3 Apr 2019
Show a small subset example with data set...I suspect you can use ismember or other logical addressing to solve the problem directly without the text step at all...but need an example to ensure we're on the same page.
Jian Shi

### Jian Shi (view profile)

6 Apr 2019
Ok, I have tried to use ismember but it is like other lookup functions that waste too much time on finding my expected value in Z at fixed point in (X,Y). For example
%every row of A is different, it can be considered as some point in R^2;
A=[1 3;2 4;2 7;6 19];
%every element of B is different, it can be considered as the value under an one-to-one mapping f(x,y) on A;
AA=cellstr(num2str(A));
B=[6;5;1;9];
BB=num2cell(B);
M=containers.Map(AA,BB);
the scale of the example is small but the real problem is huge.
i want to find the corresponding values of some point in A at the same time with less time.

로그인 to comment.

Translated by