Turning a list of answers into a single array

7 Ansichten (letzte 30 Tage)
John
John am 20 Okt. 2014
Kommentiert: John am 20 Okt. 2014
I would like to know how I can(if I can) put my answers into a single matrix/array... for example: ans=12 ans=13 and=73 ans=59 and so on, and would like it in the form [12;13;73;59]
  2 Kommentare
per isakson
per isakson am 20 Okt. 2014
How does ans get its successive values?
John
John am 20 Okt. 2014
Bearbeitet: Image Analyst am 20 Okt. 2014
I was given a file(y) with a matrix in it, with ID numbers in the 1st column and a bunch of random numbers on the right. I had to enter a users ID and receive all the random numbers that were on the same row as the ID numbers.
for x=1:size(y)
results=y(x,2);
if(ID==y(x,1));
disp(results)
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Okt. 2014
If you want to capture the output of some operation, then you need to accept it into some variable. If you don't and just let it spew out "ans = ...." to the command window then you can't put those into a variable unless you used diary() and then parsed the file - a major pain.
  2 Kommentare
Image Analyst
Image Analyst am 20 Okt. 2014
Bearbeitet: Image Analyst am 20 Okt. 2014
Regarding your loop you just added, to get the second column of y into results, do this:
results = y(:, 2);
Now, to print out results where the first column of y equals some ID number:
rowsToPrint = y(:, 1) == ID; % Logical index
fprintf('%f\n', results(rowsToPrint));
Note: no for loop is needed at all.
John
John am 20 Okt. 2014
Thank you very much ^^

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 20 Okt. 2014
Example
a=zeros(1,10);
for k=1:10
a(k)=sin(k);
end

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by