I'm currently working on a project which has numbers assigned to each letter and i'm trying to create an array of each letter with its corresponding number below it so that when i try sorting the numbers from highest to lowest the letters get arranged as well but it's not working out for me here is my code
% code
clc
clear
n=input('please enter the number of mud types you have ');
for i=1:n
x(i)=input('please enter the name of the mud in the order you want ','s');
end
for j=1:n
y(j)=input('please enter the number of the mud in the order you want ');
end
A=[x;y]

댓글 수: 1

Jan
Jan 2017년 3월 17일
편집: Jan 2017년 3월 17일
What is your question? Assigning strings to "x(i)" must fail, because strings are vectors of type char, while x(i) is a scalar.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2017년 3월 17일

1 개 추천

Using a cell string would allow to names with more than 1 character also.
n = input('please enter the number of mud types you have ');
Str = cell(1, n); % Pre-allocate!!!
y = zeros(1, n); % Pre-allocate!!!
for k = 1:n
Str{k} = input('please enter the name of the mud in the order you want ','s');
end
for k = 1:n
y(k) = input('please enter the number of the mud in the order you want ');
end
[ySorted, index] = sort(y, 'ascending');
StrSorted = Str(index);

댓글 수: 1

Ahmed Ashraf
Ahmed Ashraf 2017년 3월 17일
dude you are amazing thank you so much

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2017년 3월 17일

댓글:

2017년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by