Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

function input and output

조회 수: 1 (최근 30일)
Carl Narup
Carl Narup 2017년 10월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
Im creating a function where MPcell1 is a cell with 24 artist names of songs, unic is a cell with 11 unique artist names. I want the second row of the unic cell to add a 1 everytime unic = MPcell1 but that isn't happening. What is wrong with this?
function totsum = songsum(MPcell1, unic, c)
for i = 1:length(MPcell1)
if MPcell1(i) == unic
totsum = unic(2,c)+1;
c = c+1;
end
end
end
  댓글 수: 1
James Tursa
James Tursa 2017년 10월 5일
Every time MPcell1(i) matches any cell from the first row of unic?

답변 (1개)

Niels
Niels 2017년 10월 5일
Hi Carl Narup,
1. u get access to the cell arrays with {}
2. if you want to compare strings use strcmp
% here is 1 solution, i am not sure if you will understand it :/ with my test strings
MPcell1 = {'a', 'e', 'q', 't', 'w', 'q', 't', 'a', 'b', 'a'};
unic = {'a', 'q', 'w'};
-------------------------
function totsum = songsum(MPcell1, unic, c)
numb_artists = length(unic); % when adding a row to unic u get a matrix, using length can be confusing then
unic(2,:) = cell(1,numb_artists); % adding a row
for i = 1:numb_artists
unic{2,i} = sum(strcmp(MPcell1,unic{1,i}));
end
end
strcmp compares all elements of MPcell1 at once with each element of unic and returns a logical array (with 1 if true and 0 if false)
sum adds up all 1s to the total number of succesfull hits
But be careful, if you are not used to use logical operations on matrices the result (or further comparisons) might surprise you.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by