Effacer les filtres
Effacer les filtres

How to Extract Vector from Matrix and Get them in Print

2 vues (au cours des 30 derniers jours)
Mukund
Mukund le 12 Juin 2017
Commenté : Image Analyst le 12 Juin 2017
I have written code as below. a is input matrix. b (i) the vectors to be extracted.
clear all, clc;
a=[1 2; 3 4; 5 6; 7 8]
for i=1:4
b=a(i,:)
end
I want to extract vectors as b1, b2, b3, b4. Also get the output in print as vectors extracted are b1, b2, b3, b4 (new line) from the matrix a. How can I code it appropriately?
When I try the command b(i)=a(i:1), I get error 'assignment dimension mismatch' When I try with extra command c(i)=b(i) with or without for loop, error displayed is 'index exceeds matrix dimensions'

Réponse acceptée

KSSV
KSSV le 12 Juin 2017
a=[1 2; 3 4; 5 6; 7 8] ;
% write matrix line by line
fid = fopen('text.txt','w') ;
for i=1:4
b=a(i,:) ;
fprintf(fid,[repmat(' %f ', 1, length(b)) '\n'],b') ;
end
fclose(fid) ;
% Write whole matrix at once
dlmwrite('text.txt',a) ;
  2 commentaires
Mukund
Mukund le 12 Juin 2017
Hello KSSV, Thanks for your prompt response. I simply copied and pasted the code. The following error (image file attached) arouse regarding fprintf on Matlab 2015b. Can you please explore it bit more.
Image Analyst
Image Analyst le 12 Juin 2017
Since you did not say anything about needing a disk file, get rid of line 3 and 8-10, and change line 6 to
fprintf([repmat(' %f ', 1, length(b)) '\n'], b');
(essentially getting rid of fid).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by