NEED HELP! In an assignment A(:) = B, the number of elements in A and B must be the same.

I have to do a method which saves on an array some hash results from a SHA-384 function. That's what I'm using: http://www.mathworks.com/matlabcentral/fileexchange/31272-datahash
I need to save the results on an array 'cause then I need to use it as an input on another function.
That's what I do:
clear
N=1000;
a=zeros(1,N);
Opt = struct('Format', 'HEX', 'Method', 'SHA-384' ,'Input', 'array');
for k=1:N
a(k)=DataHash(k, Opt);
end
And that's the error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Untitled2 (line 8) a(k)=DataHash(k, Opt);
If I don't save the results from the function DataHash it works without any problem;
ans =
DE2B5CBB9F372EA7F5F3ABC7091D668ACF7B008FC0063BF068B3AAEC38B793A87B031874B838C4639A83FBC38AE95E7F
ans =
1F34BE5A6B2E8E1BA250A15793111A8AE7FEB7B891FDB28947F30A9A1047C0ED37CDC5C5B380C51743CEF536A4DF35C3
ans =
75A59F46AD758957780CF5485DDC17B3D13DC0175D21C519FB5F4E839DE79028C12C6E3FA5E41C8D08916A34EAFA6501
............
I have read some answers about the same ERROR but I couldn't fix it.
Could someone help me?
Thank's a lot.
BTW: Sorry for my english

Réponses (2)

a(k,:)=DataHash(k, Opt);

2 commentaires

I tried it but it still doesn't work:
clear
N=1000;
a=zeros(1,N);
Opt = struct('Format', 'HEX', 'Method', 'SHA-384' ,'Input', 'array');
for k=1:N
a(k,:)=DataHash(k, Opt);
end
But that's the error now:
Subscripted assignment dimension mismatch.
Error in Untitled2 (line 8)
Anyway thanks for the help
What does DataHash(k, Opt) return, precisely? What's the size, and does the size change depending on k?
Jan
Jan le 28 Oct 2015
Modifié(e) : Jan le 28 Oct 2015
You create a [1 x 1000] double array at first:
a = zeros(1,N);
Inside the loop you try to insert the data of a string with 96 characters:
a(k) = DataHash(k, Opt);
a(k) is a single element, and you cannot store 96 characters in a single element. In addition it is converted to a double, but this is not the problem here.
This cannot work also:
a(k, :) = DataHash(k, Opt);
a has 1000 elements per row due to zeros(1, 1000). Then a(k, :) is a vektor of 1000 elements and you cannot assign 96 characters to it.
Use a cell string instead:
N = 1000;
C = cell(1,N);
Opt = struct('Format', 'HEX', 'Method', 'SHA-384' ,'Input', 'array');
for k = 1:N
C{k} = DataHash(k, Opt); % Curly braces
end

2 commentaires

IT WORKS!!!! Thank you so much!!
You are welcome.
If your problem is solved, you can mark this thread by accepting the answer.

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by